我正在尝试运行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)
我正在使用nodemon
和sucrase
来运行我的服务器,如果这很重要的话。
我的Babel配置
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
]
};
我的package.json
文件
{
"type": "module",
"scripts": {
"dev": "nodemon src/server.js",
"test": "jest"
}
}
package.json
中也使用了"type": "module"
。 尽管对我来说,.cjs
和.mjs
扩展名都不起作用,但最终通过将文件更改为babel.config.json
并在package.json
中添加以下配置来解决了问题:"jest": { "modulePaths": ["<rootDir>"] }
。 - Evgeniya Manolova