我有一个使用ES6模块语法的mocha测试文件(test/test.js
),开始如下:
import {promises as fs} from 'fs'
import stepToD3 from '../src/StepToD3'
import assert from "assert"
此外,我在项目根目录下有一个babel.config.js
文件:
module.exports = {
presets: [
"@vue/app",
"@babel/preset-env"
]
}
我接下来想使用babel编译,然后用Mocha进行测试。我可以使用mocha -- require @babel/register test/test.js
命令进行测试。但是,在执行过程中,会出现以下错误:
/home/michael/Programming/cola-step/node_modules/@babel/runtime-corejs2/helpers/esm/asyncToGenerator.js:1
(function (exports, require, module, __filename, __dirname) { import _Promise from "../../core-js/promise";
^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:84:7)
at createScript (vm.js:264:10)
at Object.runInThisContext (vm.js:312:10)
at Module._compile (internal/modules/cjs/loader.js:684:28)
at Module._compile (/home/michael/Programming/cola-step/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Object.newLoader [as .js] (/home/michael/Programming/cola-step/node_modules/pirates/lib/index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/home/michael/Programming/cola-step/test/test.js:7:49)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Module._compile (/home/michael/Programming/cola-step/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Object.newLoader [as .js] (/home/michael/Programming/cola-step/node_modules/pirates/lib/index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:22:18)
at /usr/lib/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/usr/lib/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/usr/lib/node_modules/mocha/lib/mocha.js:536:10)
at Object.<anonymous> (/usr/lib/node_modules/mocha/bin/_mocha:573:18)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
at executeUserCode (internal/bootstrap/node.js:342:17)
at startExecution (internal/bootstrap/node.js:276:5)
at startup (internal/bootstrap/node.js:227:5)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
为什么我使用Babel,仍无法理解ES6的import语法?出现了什么问题?