gpt4 book ai didi

mocha.js - Lodash-es 不支持 mocha 和 babel

转载 作者:行者123 更新时间:2023-12-01 16:30:00 24 4
gpt4 key购买 nike

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

.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,因为使用它进行树摇动效果更好。

最佳答案

lodash-es 采用 esm 模块样式,而我们的公共(public)节点模块样式是 commonjs。您应该使用 lodash 来代替。

否则,请按照以下步骤使其正常工作:

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

关于mocha.js - Lodash-es 不支持 mocha 和 babel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com