SystemJS - moment is not a function
Solution 1
Simply remove the grouping (* as
) from your import statement:
import moment from 'moment';
Without digging too deeply in to the source code, it looks like moment
usually exports a function, that has all kinds of methods and other properties attached to it.
By using * as
, you're effectively grabbing all those properties and attaching them to a new object, destroying the original function. Instead, you just want the chief export (export default
in ES6, module.exports
object in Node.js).
Alternatively, you could do
import moment, * as moments from 'moment';
to get the moment function as moment
, and all the other properties on an object called moments
. This makes a little less sense when converting ES5 exports like this to ES6 style, because moment
will retain the same properties.
Solution 2
This worked for me:
import moment from 'moment/src/moment'
Related videos on Youtube
Comments
-
eestein about 4 years
I'm using
JSPM
,AngularJS
,TypeScript
,SystemJS
andES6
and my project is running pretty well... unless I try to use momentJS.This is the error I get:
TypeError: moment is not a function
This is part of the code:
import * as moment from 'moment';
More:
var momentInstance = moment(value);
If I debug it, moment is an object not a function:
This is what my moment.js JSPM package looks like:
module.exports = require("npm:[email protected]/moment.js");
I've read a lot and couldn't find a way to solve this... any ideas?
Some things I've read/tried:
How to use momentjs in TypeScript with SystemJS?
https://github.com/angular-ui/ui-calendar/issues/154
https://github.com/jkuri/ng2-datepicker/issues/5
Typescript module systems on momentJS behaving strangely
https://github.com/dbushell/Pikaday/issues/153
Thanks!
-
AlainIb over 8 yearswho did you do your injection dependency of moment in angular ?
-
eestein over 8 years@AlainIb hi, thanks for your comment. I'm sorry, but do you mean why? If so, what would you recommend as an alternative? Thanks again.
-
-
Oka over 8 years@eestein Cheers. Here's the MDN article on
import
. In my opinion, it's a little too brief right now, but it does cover most of the syntax. -
uglycoyote about 7 yearsI too had tried
import * as moment from 'moment'
which gave me the "moment is not a function" error. Changing it as you suggested toimport moment from 'moment'
just changes the error tomoment_1.default is not a function
. what is going on here? (It seems that changing the way I import has caused typescript to compile mymoment()
function call into amoment_1.default
function call.)