Lodash-es不与摩卡和巴贝尔合作

时间:2017-09-12 11:57:39

标签: mocha babel lodash

我创建了一个简单的工作区来演示问题:

.babelrc:

{
  "presets": ["es2015"]
}

的package.json

{
  "name": "mocha-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha --compilers js:babel-register"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-preset-es2015": "^6.24.1",
    "babel-register": "^6.26.0",
    "lodash-es": "^4.17.4",
    "mocha": "^3.5.3"
  }
}

测试/ index.test.js

import {describe, it} from "mocha";
import {cloneDeep} from "lodash-es";

describe("Index", () => {
    it("should work", () => {
        // Init
        const a = {a: 1};
        const b = cloneDeep(a);

        // Action

        // Test
    });
});

错误我无法解决:

> mocha-proba@1.0.0 test C:\...\mocha-demo
> mocha --compilers js:babel-register "test"

C:\...\mocha-demo\node_modules\lodash-es\lodash.js:10
export { default as add } from './add.js';
^^^^^^

SyntaxError: Unexpected token export
    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)
    at Object.require.extensions.(anonymous function) [as .js] (C:\...\mocha-demo\node_modules\babel-register\lib\node.js:152:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:/.../mocha-demo/test/index.test.js:2:1)
    at Module._compile (module.js:569:30)
    at loader (C:\...\mocha-demo\node_modules\babel-register\lib\node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (C:\...\mocha-demo\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at C:\...\mocha-demo\node_modules\mocha\lib\mocha.js:231:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\...\mocha-demo\node_modules\mocha\lib\mocha.js:228:14)
    at Mocha.run (C:\...\mocha-demo\node_modules\mocha\lib\mocha.js:514:10)
    at Object.<anonymous> (C:\...\mocha-demo\node_modules\mocha\bin\_mocha:480:18)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

Process finished with exit code 1

我该怎么做才能解决这个问题?我正在使用lodash-es,因为树摇动更好。

1 个答案:

答案 0 :(得分:1)

lodash-esesm模块样式,而我们的公共节点模块样式是commonjs。您应该改用lodash

否则,请按照以下步骤操作:

  • 安装esm:
npm install --save-dev esm
  • 针对esm运行mocha
mocha -r esm test/**/*.test.js