关于javascript:尝试使用ES6样式导入会给出“无法在模块外部使用导入语句”

Trying ES6 style import gives 'Cannot use import statement outside a module'

我试图在intellij中编写一个javascript测试,为此我需要导入一些依赖关系,并且我想使用ES6风格的导入语句,但是出现错误

/usr/local/bin/node /workspace/rr-sample/node_modules/mocha/bin/_mocha
--ui bdd --reporter"/Users/me/Library/Application Support/IntelliJIdea2019.1/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js"
tests/*.test.js /workspace/rr-sample/tests/App.test.js:3

import chai from 'chai'

^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1043:16)
at Module._compile (internal/modules/cjs/loader.js:1091:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at /workspace/rr-sample/node_modules/mocha/lib/mocha.js:334:36
at Array.forEach ()
at Mocha.loadFiles (/workspace/rr-sample/node_modules/mocha/lib/mocha.js:331:14)
at Mocha.run (/workspace/rr-sample/node_modules/mocha/lib/mocha.js:809:10)
at Object.exports.singleRun (/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:108:16)
at exports.runMocha (/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:142:13)
at Object.exports.handler (/workspace/rr-sample/node_modules/mocha/lib/cli/run.js:292:3)
at Object.runCommand (/workspace/rr-sample/node_modules/yargs/lib/command.js:242:26)
at Object.parseArgs [as _parseArgs] (/workspace/rr-sample/node_modules/yargs/yargs.js:1087:28)
at Object.parse (/workspace/rr-sample/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/workspace/rr-sample/node_modules/mocha/lib/cli/cli.js:68:6)
at Object. (/workspace/rr-sample/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:67:12)
at internal/main/run_main_module.js:17:47

到底是什么问题? 我找到了此链接(和其他链接)http://xahlee.info/js/js_import_export.html,该链接告诉您如何解决此错误,但在另一种情况下却无济于事,也无法解释问题所在。 。

万一这很有帮助,这里是我正在使用的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//const chai = require("chai");
import chai from 'chai'

const React = require("react");
const expect = chai.expect;

describe('how it works first-time test', () => {
  it('checks equality', () => {

    const val = false;
    expect(val).to.be.false;
  });

});


运行用ES6编写的Mocha测试的最简单方法是使用Mocha --require @babel/register选项即时对其进行编译(请参阅https://github.com/mochajs/mocha/wiki/compilers-deprecation#what-should-i- 使用而不是然后)。 当然,您需要确保安装相应的模块并相应地设置.babelrc

package.json:

1
2
3
4
5
6
7
"dependencies": {
 "@babel/cli":"^7.7.4",
 "@babel/core":"^7.7.4",
 "@babel/preset-env":"^7.7.4",
 "@babel/register":"^7.7.4",
...
}

.babelrc:

1
2
3
4
5
6
7
{
 "presets": [
    [
     "@babel/preset-env"
    ]
  ]
}

enter image description here

另请参阅https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei


通过在项目的源代码中添加.mocharc.yaml文件,我可以使用以下命令来使其工作:

1
require: '@babel/register'

来源:https://github.com/mochajs/mocha-examples/tree/master/packages/babel


根据文档:

https://nodejs.org/api/esm.html#esm_code_import_code_statements

因此,您必须确保将脚本作为es模块执行。

例如 使用babel-node而不是Nodejs执行脚本以启用es6或更高版本。