尝试ES6样式导入会给出“无法在模块外部使用import语句" [英] Trying ES6 style import gives 'Cannot use import statement outside a module'

查看:55
本文介绍了尝试ES6样式导入会给出“无法在模块外部使用import语句"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用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

从柴"中导入柴

^^^^^^^

SyntaxError:无法在模块外部使用import语句在wrapSafe(内部/模块/cjs/loader.js:1043:16)在Module._compile(内部/模块/cjs/loader.js:1091:27)在Object.Module._extensions..js(内部/模块/cjs/loader.js:1160:10)在Module.load(internal/modules/cjs/loader.js:976:32)在Function.Module._load(内部/模块/cjs/loader.js:884:14)在Module.require(internal/modules/cjs/loader.js:1016:19)在要求时(内部/模块/cjs/helpers.js:69:18)在/workspace/rr-sample/node_modules/mocha/lib/mocha.js:334:36在Array.forEach()在Mocha.loadFiles(/workspace/rr-sample/node_modules/mocha/lib/mocha.js:331:14)在Mocha.run(/workspace/rr-sample/node_modules/mocha/lib/mocha.js:809:10)在Object.exports.singleRun(/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:108:16)在exports.runMocha(/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:142:13)在Object.exports.handler(/workspace/rr-sample/node_modules/mocha/lib/cli/run.js:292:3)在Object.runCommand(/workspace/rr-sample/node_modules/yargs/lib/command.js:242:26)在Object.parseArgs [作为_parseArgs](/workspace/rr-sample/node_modules/yargs/yargs.js:1087:28)在Object.parse(/workspace/rr-sample/node_modules/yargs/yargs.js:566:25)在Object.exports.main(/workspace/rr-sample/node_modules/mocha/lib/cli/cli.cli:68:6)在对象.(/工作区/rr-sample/node_modules/mocha/bin/_mocha:10:23)在Module._compile(内部/模块/cjs/loader.js:1121:30)在Object.Module._extensions..js(内部/模块/cjs/loader.js:1160:10)在Module.load(internal/modules/cjs/loader.js:976:32)在Function.Module._load(内部/模块/cjs/loader.js:884:14)在Function.executeUserEntryPoint [作为runMain](内部/模块/run_main.js:67:12)在internal/main/run_main_module.js:17:47

到底是什么问题?我找到了此链接(和其他链接)

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

I am trying to write a javascript test in intellij for which I need to import some dependancies and I want to use ES6 style import statements but getting error

/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

What exactly is the issue? I found this link (and others) http://xahlee.info/js/js_import_export.html which tells you how to fix this error but in another context which doesn't help me, and it doesn't explain what the problem is.

In case it is helpful here is the code I am using.

//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;
  });

});

解决方案

The easiest way to run Mocha tests written in ES6 is compiling them on-the-fly using Mocha --require @babel/register option (see https://github.com/mochajs/mocha/wiki/compilers-deprecation#what-should-i-use-instead-then). Of course, you need to make sure to install the corresponding modules and set up the .babelrc accordingly

package.json:

"dependencies": {
  "@babel/cli": "^7.7.4",
  "@babel/core": "^7.7.4",
  "@babel/preset-env": "^7.7.4",
  "@babel/register": "^7.7.4",
...
}

.babelrc:

{
  "presets": [
    [
      "@babel/preset-env"
    ]
  ]
}

See also https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei

这篇关于尝试ES6样式导入会给出“无法在模块外部使用import语句"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆