Let's say I want to use Immutable in my project (or any given npm package). I have npm install
ed it, so it is in node_modules
. Of course, it has CommonJS exports there. I, however, want to use es6 modules in my project.
I am using Webpack to compile it all together, with the 6to5-loader to deal with es6 module syntax.
In my source file, I say import Immutable from 'immutable';
--- but this causes a problem because the es6 import
is looking for an es6 default
to have been exported, which isn't the case (for Immutable or probably almost any other npm package). The compiled code ends up looking like this: var Immutable = require('immutable')["default"];
--- which of course throws an error, since there is no default
property to find.
Can I consume the npm packages with es6 modules?
Babel.js contributor here. You're looking for the following:
import * as Immutable from 'immutable';
// compiles to:
var Immutable = require('immutable');
Note: This is with either the
common
orcommonInterop
modules option. For others, see: https://babeljs.io/docs/usage/modules/