Yoav Kadosh Yoav Kadosh - 3 years ago 151
Node.js Question

Mocha doesn't recognize dynamic imports

I'm using Mocha to test a react/webpack/babel application that is written in es6 stage-0 syntax, which includes dynamic imports. Webpack compiles it without errors, but mocha throws the following error when reaching a dynamic import syntax:

import('path/to/file').then(...)
^^^^^^
SyntaxError: Unexpected token import


The command that i'm running is:

$ mocha --compilers js:babel-register src/**/*.test.js


And in my
.babelrc
I have to following:

{
"presets": ["es2015", "react", "stage-0"]
}

Answer Source

As @louis has mentioned, this is a node issue. The solution is to add the dynamic-import-node plugin to babel:

{
  "presets": ["es2015", "react", "stage-0"],
  "plugins": ["dynamic-import-node"]
}

Which will add support for the import(...) syntax in node.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download