Moment is not a function

This is the first NPM package I’ve utilized with meteor.

The steps I performed are as follows:

  1. Read this page: https://guide.meteor.com/using-npm-packages.html
    (Coincidentally uses Moment as an example, and yet…)

  2. Installed the package via:

    meteor npm install --save moment

  3. Added the following to the top of my js file:

    import moment from ‘moment’;

  4. Added “var newMoment = moment();”

  5. Browser console says “moment is not a function.”

Am I doing something wrong?

Hello @Tomwa,

After installing package you can import moment in below way and use.

//Add below line in Header
var moment = require('moment');

// and use moment() in your controller.
var startDate = moment().startOf('month');

Thanks,
Tarun Sharma
Sr. Developer @ AIMDek Technologies Pvt Ltd
+91 9510657842

import moment from 'moment'; is equivalent to const moment = require('moment');, so either will work.

@Tomwa : did you import in a client (or shared) file? If you imported only on the server, then moment won’t be available in your client. In your specific example, your newMoment would actually be set to the current date and time (it’s equivalent to var newMoment = new Date();) - is that what you expected?


//Add below line in Header
var moment = require(‘moment’);

// and use moment() in your controller.
var startDate = moment().startOf(‘month’);


Replacing, the import with the NPM style require didn’t solve it. Error message remains the same.

@Tomwa : did you import in a client (or shared) file? If you imported only on the server, then moment won’t be available in your client.

I Imported it in a .js file on the Client (Under the Client folder), I have no need of it server side as it’s not privileged information in any way.

In your specific example, your newMoment would actually be set to the current date and time (it’s equivalent to var newMoment = new Date():wink: - is that what you expected?

Yes, though technically I was just trying to see if I imported it correctly. I wished to use it to do necessary date\time calculations (i.e. 5 months, 3 weeks, 6 months since x) without having to account for varying month sizes (and leap years, etc.) myself.

I have yet to get the “moment is not a function” issue resolved.

I’ve just tried your code myself in a new Meteor project and it works as expected, so I think there’s something else in your project that’s not apparent.

Please put your code into a github repo so we can look at the structure as well as the code.

try import {Moment} from ‘moment’;

That is the guaranteed way to get the “moment is not a function” error. However @Tomwas followed the guide example which uses the correct method:

import moment from ‘moment’;

// this is equivalent to the standard node require:
const moment = require(‘moment’);

I’ll see if I can’t get the code up sometime here shortly, I wrote simple JS to account for dates\times in the meantime.

Thought I’d reply here, not sure how or why but the issue has resolved itself.

I can use MomentJS as expected.