How to import moment.js in ES6 using npm? How to import moment.js in ES6 using npm? angularjs angularjs

How to import moment.js in ES6 using npm?


Currently, to use ES6 module syntax, you need to use a transpiler e.g. Babel, since node.js and most browsers do not yet support ES6 module syntax.

Babel and webpack are great for this. Here is a nice example:

Once you have it configured properly, you invoke moment as such:

import moment from 'moment';console.log(moment.now());


Since version 2.10.0 moment is written in ES6 :

import moment from './node_modules/moment/src/moment';// Note the 'src/' to import the ES6 version and not the CJS build

However for now it seems there is no way to import only a subset of functions, you have to import the whole thing.


The option, that worked for me is:

import * as moment from 'moment';