Call to undefined function asset() in Laravel project - javascript

I am trying to load my app.js file inside my welcome.blade.php but I get the following error:
Call to undefined function asset()
This is the instruction I used <script type="text/javascript" src="{{ asset('public/js/app.js') }}"></script>
What can be the problem? I also tried to run the same line without asset() and it does not work. My .js file is in the location I specified.
Thank you so much!

Process:
I am using Laravel 5.5 and had the same problem recently so I did a bit of debugging. Here are the steps:
Since the error was Call to undefined function asset() I checked the helpers.php in the Laravel repo to see if it existed. https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/helpers.php#L127 and it did.
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* #param string $path
* #param bool $secure
* #return string
*/
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
}
I looked on my local copy of Laravel and see if that same line existed, it did.
However my local copy was different than what was on the repo. This is how it looked:
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* #param string $path
* #param bool $secure
* #return string
*/
function mixasset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
}
At some point, Laravel Mix seems to have changed asset() to mixasset().
Result:
Try using mixasset() as opposed to asset(). Otherwise delete your vendors folder and rebuild it by running composer install. This should get you the latest copy of your Laravel version with the correct asset() helper method.

The asset() is already point to your public folder so, don't specify the public in your asset()
Change your asset('public/js/app.js') to asset('js/app.js')

My Err: Call to undefined function set_active()...., when I move some functions in Laravel 5.2 to Laravel 6.x ...
My solution:
1. Added below in composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/helpers.php" . => New added
]
}
2.run composer install
it works for me.

Related

How to enable IDE auto-completion for java-script npm packages (which do not have .d.ts files) in type-script?

I have created an empty react-native project but using type-script (like react-native init MyApp --template typescript), then installed on that react-navigation-tabs package, and the problem which I am facing is:
TS7016: Could not find a declaration file for module 'react-navigation-tabs'.
'.../node_modules/react-navigation-tabs/src/index.js' implicitly has an 'any' type.
Try `npm install #types/react-navigation-tabs` if it exists
or add a new declaration (.d.ts) file containing `declare module 'react-navigation-tabs';`
I did also try npm install --save-dev #types/react-navigation-tabs, and since no such package did exist,
for now I did workaround it like suggested in another post.
But coming back to the question in the title, it is really painful, I mean the information that the IDE needs to provide auto-completion are there, in the node_modules/react-navigation-tabs/src/index.js file, like:
/* #flow */
/* eslint-disable import/no-commonjs */
module.exports = {
/**
* Navigators
*/
get createBottomTabNavigator() {
return require('./navigators/createBottomTabNavigator').default;
},
get createMaterialTopTabNavigator() {
return require('./navigators/createMaterialTopTabNavigator').default;
},
/**
* Views
*/
get BottomTabBar() {
return require('./views/BottomTabBar').default;
},
get MaterialTopTabBar() {
return require('./views/MaterialTopTabBar').default;
},
/**
* Utils
*/
get createTabNavigator() {
return require('./utils/createTabNavigator').default;
},
};
How can we make the IDE (e.g. VS Code) show auto-completion using the data it can find in the existing node_modules/react-navigation-tabs/src/index.js file ?
Edit: Also, created a request/question on github.

SuiteScript2.0 - Including a custom file

Im working with suitescript 2.0 (netsuite) and Im wondering how would I go about including a custom class (object) using it's new API. For example I'm trying to include a controller class but getting a "module not found" warning. See snippet below
/**
*#NApiVersion 2.x
*#NScriptType Restlet
*/
define(['N/record', 'N/error', "src/My_Controller"],
function (record, error, My_Controller) {
var controller = new My_Controller();
...
The error message is: Module does not exist: src/My_Controller.js when in fact it is there. Is this the correct way to do it?
The NetSuite help center has nothing about inclusion of custom/ancillary javascript
You reference custom modules by their path in the File Cabinet. This can be either relative to the current file or relative to the root of the File Cabinet. So it will look something like:
define(['N/record', 'N/error', '/SuiteScripts/my-project/src/My_Controller'], ...)
or:
define(['N/record', 'N/error', './src/My_Controller'], ...)
Assuming that src is in the same directory as this file.

Jenkins integration with Jest

Is there a way to have Jenkins integration in the Javascript Jest testing framework that is built on top of Jasmine?
I've tried to integrate Jest with jasmine-reporters, but didn't manage to get a JUnit XML output. I installed the reporters for Jasmine 1.3 with npm install jasmine-reporters#~1.0.0 and then in my setupTestFrameworkScriptFile:
require('jasmine-reporters');
jasmine.VERBOSE = true;
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter({
savePath: "output/"
}));
When I run jest I get NodeJS attempt: Arguments to path.join must be strings or NodeJS attempt: Object [object Object] has no method 'join'.
I've managed to get a working version of it in this repo. The problem was I was not mocking path and fs in the test file.
You're using the syntax of jasmine-reporters 2.x with the 1.x branch. Specifically, you are passing an object of options but you need to send positional arguments.
Don't do this:
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter({
savePath: "output/"
}));
Instead, you should do this:
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter("output/"));
You can check out the source for the list of available options. Here are the current options:
/**
* Generates JUnit XML for the given spec run.
* Allows the test results to be used in java based CI
* systems like CruiseControl and Hudson.
*
* #param {string} [savePath] where to save the files
* #param {boolean} [consolidate] whether to save nested describes within the
* same file as their parent; default: true
* #param {boolean} [useDotNotation] whether to separate suite names with
* dots rather than spaces (ie "Class.init" not
* "Class init"); default: true
* #param {string} [filePrefix] is the string value that is prepended to the
* xml output file; default: 'TEST-'
* #param {boolean} [consolidateAll] whether to save test results from different
* specs all in a single file; filePrefix is then the whole file
* name without extension; default: false
*/
var JUnitXmlReporter = function(savePath, consolidate, useDotNotation, filePrefix, consolidateAll) {
/* ... */
}
Looks like jest-junit is also a option.

How to handle blocked scripts in requirejs

I am using require js to load google analytics.
In config I have
requirejs.config({
"paths": {
"ga": "//www.google-analytics.com/analytics",
...
And I have a module that depends on ga that initialises analytics.
Everything works fine until someone uses a browser plugin that blocks google analytics.
When that happens, the resulting javascript error breaks everything.
failed to load resource : blocked by clien
uncaught error: script error for: ga
How can I tell requirejs not to have a fit if a certain module fails to load?
How can you make a module optional?
Thanks.
require takes a 3rd argument which is an error callback, so you can assign window.ga to a function which always returns undefined. This avoids errors when calling google analytics functions elsewhere in your code.
require(['ga'], function(data) {
window.ga('create', 'UA-XXXXXXXX-X');
window.ga('send', 'pageview');
}, function() {
window.ga = function(){};
});
You could require the module within your own module code but outside of the module definiton requirements, but this does mean you can't quite as easily chain on dependencies you need. i.e.
define([ /* Normal dependencies here ... */], function() {
try {
require(['ga']);
} catch (error) {
// Handle lack of GA if needed
}
};
Alternatively you'd have to write your own module wrapper which synchronously blocks as it attempts the above, then returns GA if it was successful, or null otherwise.
I found the best way is to use the array notation for path definitions. This way you can define the external URL for the module, and a local fallback, in your requirejs path configuration. No need for additional try/catch blocks or module-specific error handling.
Documentation link:
http://requirejs.org/docs/api.html#pathsfallbacks
I defined a module named noop which defines an empty function, and then set up my paths like this:
requirejs.config({
"paths": {
"ga": [
"//www.google-analytics.com/analytics",
"util/noop"
],
...
The best solution, It works for me.
Open lib/mage/requirejs/resolver.js file.
/**
* Checks if provided module has unresolved dependencies.
*
* #param {Object} module - Module to be checked.
* #returns {Boolean}
*/
function isPending(module) {
return !!module.depCount;
}
Replace with this bellow code:
/**
* Checks if provided module is rejected during load.
*
* #param {Object} module - Module to be checked.
* #return {Boolean}
*/
function isRejected(module) {
return registry[module.id] && (registry[module.id].inited || registry[module.id].error);
}
/**
* Checks if provided module has unresolved dependencies.
*
* #param {Object} module - Module to be checked.
* #returns {Boolean}
*/
function isPending(module) {
if (!module.depCount) {
return false;
}
return module.depCount > _.filter(module.depMaps, isRejected).length;
}
Thanks

Change sails.js EJS views to use .html extensions instead of .ejs extensions?

Is it possible to configure sails.js apps to use .html extentions rather than .ejs (but still use the ejs view engine)?
sails new app creates ./views/home/index.ejs and ./views/layout.ejs.
I'd like to change the extensions to .html but keep everything else working the same way.
ie: I would now have ./views/home/index.html and ./views/layout.html, and the home page would still be injected into the layout page, as per normal.
How can I configure this please?
In your config/views.js:
engine: {
ext: 'html',
fn: require('ejs').renderFile
},
Seems though that the future support for this feature is not guaranteed, since they removed this from docs, so use with caution.
Another approach
Sails provides EJS templating by default.To override this and to use .html files , here is a simple solution. In your Sails App , go to config/routes.js. You will see following code there
module.exports.routes = {
/***************************************************************************
* *
* Make the view located at `views/homepage.ejs` (or `views/homepage.jade`, *
* etc. depending on your default view engine) your home page. *
* *
* (Alternatively, remove this and add an `index.html` file in your *
* `assets` directory) *
* *
***************************************************************************/
'/': {
view: 'homepage'
}
/***************************************************************************
* *
* Custom routes here... *
* *
* If a request to a URL doesn't match any of the custom routes above, it *
* is matched against Sails route blueprints. See `config/blueprints.js` *
* for configuration options and examples. *
* *
***************************************************************************/
};
Remove the route to '/' as shown below . Keep it blank
New routes.js will look like
module.exports.routes = {
//Remove '/' :)
};
Okay !!! now it’s done you can use your HTML files in Sails app . Put your index.html in assets folder . Sails will now load views from here :)
In latest sails.js 0.11, this also valid:
engine: 'ejs',
extension: 'html',
To check how they do this, in /node_modules/sails/lib/hooks/views/configure.js:
if (typeof sails.config.views.engine === 'string') {
var viewExt = sails.config.views.extension || sails.config.views.engine;
sails.config.views.engine = {
name: sails.config.views.engine,
ext: viewExt
};
}

Categories