Trying ES6 style import gives ‘Cannot use import statement outside a module’

The easiest way to run Mocha tests written in ES6 is compiling them on-the-fly using Mocha --require @babel/register option (see https://github.com/mochajs/mocha/wiki/compilers-deprecation#what-should-i-use-instead-then). Of course, you need to make sure to install the corresponding modules and set up the .babelrc accordingly

package.json:

"dependencies": {
  "@babel/cli": "^7.7.4",
  "@babel/core": "^7.7.4",
  "@babel/preset-env": "^7.7.4",
  "@babel/register": "^7.7.4",
...
}

.babelrc:

{
  "presets": [
    [
      "@babel/preset-env"
    ]
  ]
}

enter image description here

See also https://dev.to/bnorbertjs/my-nodejs-setup-mocha–chai-babel7-es6-43ei

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)