Nodemon和Jest仅在异步运行Babel时受支持。

4

我正在尝试运行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"
  }
}
1个回答

9

我认为问题在于你的package.json文件中使用了ES6模块,但是你的Babel配置使用了CommonJS(而不是ES6模块)的module.exports

我将我的babel.config.js文件重命名为babel.config.cjs,这解决了问题。我猜你也可以将module.exports改成export default,但我没有尝试过。


6
哇,将它从js更改为cjs确实奏效了,非常感谢!注意:仅将其更改为“export default”并不能奏效。 - Leonardo Bezerra
我在package.json中也使用了"type": "module"。 尽管对我来说,.cjs.mjs扩展名都不起作用,但最终通过将文件更改为babel.config.json并在package.json中添加以下配置来解决了问题:"jest": { "modulePaths": ["<rootDir>"] } - Evgeniya Manolova

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接