运行 Mocha 与 node_modules/ 中的 ES6 文件时出错
Error when running Mocha with ES6 files in node_modules/
我有一个使用 ES6 语法的文件,当我尝试 运行 Mocha 时会导致以下错误:
import 'test';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Module._extensions..js (module.js:580:10)
我 运行 我使用以下 npm 命令进行测试:
"test": "mocha --reporter progress --require babel-core/register tools/testSetup.js \"**/*_spec.jsx\""
这是我的 .babelrc 文件:
{
"presets": ["es2015", "react"]
}
使用 ES6 语法的文件包含在通过 git 引用的私有模块下的 node_modules/ 中。有什么方法可以 运行 Mocha 而不必从这个私有模块中手动删除 ES6 语法吗?当我 运行 Mocha.
时,我项目中的所有常规 ES6 文件都不会导致任何问题
babel-register
是正确的方法,但不幸的是,默认情况下它会忽略 node_modules
中的代码:https://babeljs.io/docs/usage/babel-register/#ignores-node_modules-by-default
您可以通过一个选项告诉它不要这样做,如下所示:
require("babel-register")({
// This will override `node_modules` ignoring - you can alternatively pass
// an array of strings to be explicitly matched or a regex / glob
ignore: false
});
这在命令行上不起作用,因此您可能必须将该行代码添加到您的 testSetup.js
文件中(使其成为文件中的第一行)并删除 --require babel-core/register
来自 command-line 选项。
npm i @babel/register -D
修改package.json
"mocha": "./node_modules/.bin/mocha --compilers js:@babel/register",
这里已经向 ts-node 提交了一个问题 https://github.com/TypeStrong/ts-node/issues/617。总结一下,默认情况下,ts-node 不会转译 node_modules 包。如果你的 ts 文件有一个指向 node_module 包之一的 import 语句,那么遵循依赖规则,ts-node 将转译它(如果你在 node10 或更高版本上。)如果你的节点版本低于10,那你还是会报上面的错误。
正如上面提到的blakeembrey link,
TS Node does not compile files in node_modules by default. If you want to consume ES modules, you should update to a node.js version that supports ES modules natively, use something like esm or alter the default behaviour by changing --ignore.
在理想情况下,您不应该拥有带有 ts 文件的 npm 包。应该是JS文件。如此处所述https://github.com/TypeStrong/ts-node#import-statements
我有一个使用 ES6 语法的文件,当我尝试 运行 Mocha 时会导致以下错误:
import 'test';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Module._extensions..js (module.js:580:10)
我 运行 我使用以下 npm 命令进行测试:
"test": "mocha --reporter progress --require babel-core/register tools/testSetup.js \"**/*_spec.jsx\""
这是我的 .babelrc 文件:
{
"presets": ["es2015", "react"]
}
使用 ES6 语法的文件包含在通过 git 引用的私有模块下的 node_modules/ 中。有什么方法可以 运行 Mocha 而不必从这个私有模块中手动删除 ES6 语法吗?当我 运行 Mocha.
时,我项目中的所有常规 ES6 文件都不会导致任何问题babel-register
是正确的方法,但不幸的是,默认情况下它会忽略 node_modules
中的代码:https://babeljs.io/docs/usage/babel-register/#ignores-node_modules-by-default
您可以通过一个选项告诉它不要这样做,如下所示:
require("babel-register")({
// This will override `node_modules` ignoring - you can alternatively pass
// an array of strings to be explicitly matched or a regex / glob
ignore: false
});
这在命令行上不起作用,因此您可能必须将该行代码添加到您的 testSetup.js
文件中(使其成为文件中的第一行)并删除 --require babel-core/register
来自 command-line 选项。
npm i @babel/register -D
修改package.json
"mocha": "./node_modules/.bin/mocha --compilers js:@babel/register",
这里已经向 ts-node 提交了一个问题 https://github.com/TypeStrong/ts-node/issues/617。总结一下,默认情况下,ts-node 不会转译 node_modules 包。如果你的 ts 文件有一个指向 node_module 包之一的 import 语句,那么遵循依赖规则,ts-node 将转译它(如果你在 node10 或更高版本上。)如果你的节点版本低于10,那你还是会报上面的错误。
正如上面提到的blakeembrey link,
TS Node does not compile files in node_modules by default. If you want to consume ES modules, you should update to a node.js version that supports ES modules natively, use something like esm or alter the default behaviour by changing --ignore.
在理想情况下,您不应该拥有带有 ts 文件的 npm 包。应该是JS文件。如此处所述https://github.com/TypeStrong/ts-node#import-statements