Skip to content
Advertisement

Moment js undefined Laravel

I’m trying to use moment.js in my laravel project, not using vue, but still importing the package via npm.

My app.js file:

JavaScript

in my webpack.pix.js file looks like this:

JavaScript

And then in my custom.js file I’ve just got this:

JavaScript

I’m getting the following error:

JavaScript

I’ve verified that moment is in my compiled app.js file. So why can’t my custom.js file see it, and what do I need to do to fix it?

Advertisement

Answer

You’re currently not assigning moment to anything. As per the docs, you should assign it to a variable to be used:

var moment = require(‘moment’);
moment().format();

However, this will not work if you want to use it outside the scope of your app.js file. One way to do this would be to add it to the window object:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement