gpt4 book ai didi

node.js - Nodemon 和 Jest,仅在异步运行 Babel 时支持

转载 作者:行者123 更新时间:2023-12-05 09:33:47 35 4
gpt4 key购买 nike

我正在尝试运行 Jest,但此错误一直阻止我运行任何测试:

Error while loading config - 
You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.

at loadCjsOrMjsDefault (node_modules/@babel/core/lib/config/files/module-types.js:59:13)
at loadCjsOrMjsDefault.next (<anonymous>)
at readConfigJS (node_modules/@babel/core/lib/config/files/configuration.js:174:47)
at readConfigJS.next (<anonymous>)
at Function.<anonymous> (node_modules/@babel/core/lib/gensync-utils/async.js:16:3)
at evaluateSync (node_modules/gensync/index.js:251:28)
at Function.sync (node_modules/gensync/index.js:89:14)
at sync (node_modules/@babel/core/lib/gensync-utils/async.js:56:25)
at sync (node_modules/gensync/index.js:182:19)

我正在使用 nodemonsucrase 来运行我的服务器,如果相关的话。

我的 babel 配置

module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
]
};

我的package.json

{
"type": "module",
"scripts": {
"dev": "nodemon src/server.js",
"test": "jest"
}
}

最佳答案

我认为问题是你的 package.json 说你正在使用 ES6 模块,但你的 Babel 配置使用的是 CommonJS(不是 ES6 模块)的 module.exports ).

我将我的 babel.config.js 重命名为 babel.config.cjs 并且这解决了问题。我想您也可以将 module.exports 更改为 export default 但我还没有尝试过。

关于node.js - Nodemon 和 Jest,仅在异步运行 Babel 时支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67051343/

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