node.js - Babel unexpected token import when running mocha tests -


the solutions offered in other related questions, such including proper presets (es2015) in .babelrc, implemented in project.

i have 2 projects (lets call them , b) both use es6 module syntax. in project a, i'm importing project b installed via npm , lives in node_modules folder. when run test suite project a, i'm getting error:

syntaxerror: unexpected token import

which preceded alleged erroneous line of code project b:

(function (exports, require, module, __filename, __dirname) { import createbrowserhistory 'history/lib/createbrowserhistory';

the iife appears npm or possibly babel related since source file contains "import createbrowserhistory 'history/lib/createbrowserhistory'; unit tests in project b's test suite runs fine, , if remove project b dependency project a, test suite (still using es6 imports internal project modules) works fine.

full stack trace:

 syntaxerror: unexpected token import     @ exports.runinthiscontext (vm.js:53:16)     @ module._compile (module.js:374:25)     @ module._extensions..js (module.js:405:10)     @ object.require.extensions.(anonymous function) [as .js] (/projecta/node_modules/babel-register/lib/node.js:138:7)     @ module.load (module.js:344:32)     @ function.module._load (module.js:301:12)     @ module.require (module.js:354:17)     @ require (internal/module.js:12:17)     @ object.<anonymous> (actioncreators.js:4:17)     @ module._compile (module.js:398:26)     @ loader (/projecta/node_modules/babel-register/lib/node.js:130:5)     @ object.require.extensions.(anonymous function) [as .js] (/projecta/node_modules/babel-register/lib/node.js:140:7)     @ module.load (module.js:344:32)     @ function.module._load (module.js:301:12)     @ module.require (module.js:354:17)     @ require (internal/module.js:12:17)     @ object.<anonymous> (/projecta/src/components/core/wrapper/wrapper.js:28:23)     @ module._compile (module.js:398:26)     @ loader (/projecta/node_modules/babel-register/lib/node.js:130:5)     @ object.require.extensions.(anonymous function) [as .js] (/projecta/node_modules/babel-register/lib/node.js:140:7)     @ module.load (module.js:344:32)     @ function.module._load (module.js:301:12)     @ module.require (module.js:354:17)     @ require (internal/module.js:12:17)     @ object.<anonymous> (/projecta/src/components/core/wrapper/wrapperspec.js:15:16)     @ module._compile (module.js:398:26)     @ loader (/projecta/node_modules/babel-register/lib/node.js:130:5)     @ object.require.extensions.(anonymous function) [as .js] (/projecta/node_modules/babel-register/lib/node.js:140:7)     @ module.load (module.js:344:32)     @ function.module._load (module.js:301:12)     @ module.require (module.js:354:17)     @ require (internal/module.js:12:17)     @ /projecta/node_modules/mocha/lib/mocha.js:219:27     @ array.foreach (native)     @ mocha.loadfiles (/projecta/node_modules/mocha/lib/mocha.js:216:14)     @ mocha.run (/projecta/node_modules/mocha/lib/mocha.js:468:10)     @ object.<anonymous> (/projecta/node_modules/mocha/bin/_mocha:403:18)     @ module._compile (module.js:398:26)     @ object.module._extensions..js (module.js:405:10)     @ module.load (module.js:344:32)     @ function.module._load (module.js:301:12)     @ function.module.runmain (module.js:430:10)     @ startup (node.js:141:18)     @ node.js:980:3 

here test command package.json:

"test": "mocha --compilers js:babel-core/register '+(test|src)/**/*spec.js'" 

this stackoverflow post similar doesn't offer solution use of command line: import module node_modules babel failed

the easiest way solve problem is:

  1. npm install babel-preset-es2015 --save-dev
  2. add .babelrc root of project contents:

    {  "presets": [ "es2015" ] } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -