Carlo
Carlo

Reputation: 401

nodejs mocha es6 modules unexpected token export without babel

I am trying to run with some unit test with mocha on ES6 modules with the following command:

node --experimental-modules .\node_modules\mocha\bin\mocha --reporter progress "client/assets/utils/url-utils.test.mjs"

But I get the following error:

(function (exports, require, module, __filename, __dirname) { export class URLUtils {
                                                              ^^^^^^
SyntaxError: Unexpected token export

Isn't the --experimental-modules param supposed to let me use ES6 modules? I would prefer to avoid using Babel or other transpilers for this.

Node version: 11.7.0

Mocha version: 5.2.0

Upvotes: 5

Views: 687

Answers (1)

vinyll
vinyll

Reputation: 11409

In order to run mocha with experimental modules, you should:

  1. install mocha-erm: npm install mocha-esm --only-dev
  2. run your tests with npx mocha client/assets/utils/url-utils.test.mjs -r esm

Upvotes: 1

Related Questions