SyntaxError:带有摩卡和babel的意外标识符

时间:2019-01-14 05:50:36

标签: javascript node.js mocha babel

我有一个以ES6模块语法开头的摩卡测试文件(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导入语法?怎么了?

1 个答案:

答案 0 :(得分:0)

在这种情况下,我在NodeJS上使用mocha运行测试,但是我的babel.config.js被设置为为浏览器生成代码。为了解决这个问题,我必须创建两个不同的babel设置,然后在运行适当的命令之前导出正确的NODE_ENV值(例如,NODE_ENV=test mocha --require @babel/register用于测试,NODE_ENV=production用于Webpack生产版本) )。

这是我的新babel.config.js

module.exports = {
    'env': {
        'production': {
            'presets': [
                [
                    '@vue/app',
                    {
                        // "modules": false,
                        'targets': ['ie >= 9']
                    },
                    '@babel/env'
                ]
            ],
            'comments': false
        },
        'test': {
            'presets': [
                [
                    '@babel/env',
                    {'targets': {'node': 'current'}}
                ]
            ]
        }
    }
}