我的项目中已经实现了其他相关问题中提供的解决方案,例如在.babelrc中包含正确的预设(es2015)。
我有两个项目(让它们分别称为A和B)都使用ES6模块语法。在项目A中,我将导入通过npm安装的项目B,该项目位于node_modules文件夹中。当我为项目A运行测试套件时,出现错误:
SyntaxError:意外的令牌导入
在此之前,项目B声称存在以下错误代码行:
(函数(导出,需求,模块,__ filename,__ dirname)){从'history / lib / createBrowserHistory'导入createBrowserHistory;
Iife似乎与npm或可能与babel有关,因为我的源文件仅包含“从'history / lib / createBrowserHistory'导入import createBrowserHistory”;项目B的测试套件中的单元测试运行良好,并且如果我将Project B作为依赖项从项目A,然后是我的测试套件(仍然对内部项目模块使用es6导入)就可以了。
全栈跟踪:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Module._extensions..js (module.js:405:10)
at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (actionCreators.js:4:17)
at Module._compile (module.js:398:26)
at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapper.js:28:23)
at Module._compile (module.js:398:26)
at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapperSpec.js:15:16)
at Module._compile (module.js:398:26)
at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at /ProjectA/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/ProjectA/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/ProjectA/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/ProjectA/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:980:3
这是我来自package.json的测试命令:
"test": "mocha --compilers js:babel-core/register '+(test|src)/**/*Spec.js'"
此StackOverflow帖子类似,但未为我使用命令行提供解决方案: 使用babel从node_modules导入模块,但失败