{"id":576,"hash":"bd57d8c3ea7e69674e469365eaeb6d84abb06732a21cf70db1222f79ccc04e6c","pattern":"Babel unexpected token import when running mocha tests","full_message":"The solutions offered in other related questions, such as including the proper presets (es2015) in .babelrc, are already implemented in my project.\n\nI have two projects (lets call them A and B) which both use ES6 module syntax.  In Project A, I'm importing Project B which is installed via npm and lives in the node_modules folder. When I run my test suite for Project A, I'm getting the error: \n\n  SyntaxError: Unexpected token import\n\nWhich is preceded by this alleged erroneous line of code from Project B:\n\n  (function (exports, require, module, __filename, __dirname) { import\n  createBrowserHistory from 'history/lib/createBrowserHistory';\n\nThe iife appears to be something npm or possibly babel related since my source file only contains \"import createBrowserHistory from 'history/lib/createBrowserHistory'; The unit tests in Project B's test suite runs fine, and if I remove Project B as a dependency from Project A, my test suite then (still using es6 imports for internal project modules) works just fine.\n\nFull Stack Trace:\n\n SyntaxError: Unexpected token import\n    at exports.runInThisContext (vm.js:53:16)\n    at Module._compile (module.js:374:25)\n    at Module._extensions..js (module.js:405:10)\n    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:138:7)\n    at Module.load (module.js:344:32)\n    at Function.Module._load (module.js:301:12)\n    at Module.require (module.js:354:17)\n    at require (internal/module.js:12:17)\n    at Object.<anonymous> (actionCreators.js:4:17)\n    at Module._compile (module.js:398:26)\n    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)\n    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)\n    at Module.load (module.js:344:32)\n    at Function.Module._load (module.js:301:12)\n    at Module.require (module.js:354:17)\n    at require (internal/module.js:12:17)\n    at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapper.js:28:23)\n    at Module._compile (module.js:398:26)\n    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)\n    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)\n    at Module.load (module.js:344:32)\n    at Function.Module._load (module.js:301:12)\n    at Module.require (module.js:354:17)\n    at require (internal/module.js:12:17)\n    at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapperSpec.js:15:16)\n    at Module._compile (module.js:398:26)\n    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)\n    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)\n    at Module.load (module.js:344:32)\n    at Function.Module._load (module.js:301:12)\n    at Module.require (module.js:354:17)\n    at require (internal/module.js:12:17)\n    at /ProjectA/node_modules/mocha/lib/mocha.js:219:27\n    at Array.forEach (native)\n    at Mocha.loadFiles (/ProjectA/node_modules/mocha/lib/mocha.js:216:14)\n    at Mocha.run (/ProjectA/node_modules/mocha/lib/mocha.js:468:10)\n    at Object.<anonymous> (/ProjectA/node_modules/mocha/bin/_mocha:403:18)\n    at Module._compile (module.js:398:26)\n    at Object.Module._extensions..js (module.js:405:10)\n    at Module.load (module.js:344:32)\n    at Function.Module._load (module.js:301:12)\n    at Function.Module.runMain (module.js:430:10)\n    at startup (node.js:141:18)\n    at node.js:980:3\n\nHere is my test command from package.json:\n\n\"test\": \"mocha --compilers js:babel-core/register '+(test|src)/**/*Spec.js'\"\n\nThis StackOverflow post is similar but doesn't offer a solution for my use of the command line:\nimport a module from node_modules with babel but failed","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"It seems the only solution is to explicitly include: \n\nrequire('babel-core/register')({\n  ignore: /node_modules/(?!ProjectB)/\n}); \n\nin a test helper file, and pass that along to mocha in my test command:\n\nmocha --require ./test/testHelper.js...\n\nThe final solution:\n\nAdd registerBabel.js: a separate file whose job is to require babel-core/register...\n\nrequire('babel-core/register')({\n  ignore: /node_modules/(?!ProjectB)/\n});\n\nAdd an entry.js if your application also relies on babel-node. This acts as a wrapper for your es6 containing application.\n\nrequire('./registerBabel');\nrequire('./server'); // this file has some es6 imports\n\nYou would then run your application with node entry\n\nFor mocha testing, testHelper.js should require registerBabel.js as well to initialize babel support at run time.\n\nrequire('./registerBabel');\n\nAnd run your mocha tests with mocha --require ./testHelper.js '+(test)/**/*Spec.js'\n\nThis will recursively test any file ending in \"Spec.js\" within \"./test\". Substitute the pattern with one matching the specs in your project.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/35040978/babel-unexpected-token-import-when-running-mocha-tests","votes":98,"created_at":"2026-04-19T04:51:22.573560+00:00","updated_at":"2026-04-19T04:51:22.573560+00:00"}