【问题标题】:Nodemon and Jest, only supported when running Babel asynchronouslyNodemon 和 Jest,仅在异步运行 Babel 时支持
【发布时间】:2021-05-15 14:09:36
【问题描述】:

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

【问题讨论】:

    标签: node.js jestjs nodemon


    【解决方案1】:

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

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

    【讨论】:

    • 哇,将它从 js 更改为 cjs 确实有效,非常感谢!注意:只是将其更改为导出默认值是行不通的。
    猜你喜欢
    • 2022-01-18
    • 2017-01-13
    • 1970-01-01
    • 2019-03-11
    • 2015-03-20
    • 2018-06-11
    • 2022-11-09
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多