gpt4 book ai didi

node.js - Jest : Couldn't find preset "@babel/env" relative to directory

转载 作者:搜寻专家 更新时间:2023-10-31 23:50:17 25 4
gpt4 key购买 nike

我正在尝试在 ES6 项目中设置 Jest。没走多远,因为我一运行 jest,就开始遇到问题。

.babelrc

{
"presets": [
"@babel/env"
]
}

上面的代码在运行 Jest 时抛出一个 Couldn't find preset "@babel/env"relative to directory 错误。我猜这很好,因为它应该是 @babel/preset-env 无论哪种方式? (虽然两者都适用于我的其余设置......)

但是!当我将预设更改为 @babel/preset-env 时,我收到的错误非常令人困惑:

Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel. (While processing preset: "/Users/.../node_modules/@babel/preset-env/lib/index.js")

这很奇怪,因为我的 Babel 和 Jest devDeps 设置为:

"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/register": "^7.0.0",
"babel-jest": "^23.6.0"
}

据我所知,该错误消息完全没有意义。我的下一个想法是检查我的全局 Babel CLI 安装,它是 babel-cli@6.26.0。更接近,但它不是错误消息中所述的 6.26.3。我还尝试用最新的 @babel/cli 替换全局包,但没有任何区别。

我可能在这里遗漏了什么?

最佳答案

啊,解决了。由于一些非常奇怪的原因,我不得不在我的本地设置中包含 'babel-core@^7.0.0-0',这是我在 babel-jest 中找到的。 npmjs 页面(如下截图):

2]

基本上,运行 npm i -D 'babel-core@^7.0.0-0' 解决了我的问题,让我的 devDeps 变成这样:

"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.0",
"@babel/register": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
}

包括 @babel/corebabel-core 对我来说似乎很疯狂,但是,嘿,它有效!我猜 6.26.3 错误消息仍然是个谜,因为我根本找不到那个版本的任何 Babel 包,无论是本地还是全局...

关于node.js - Jest : Couldn't find preset "@babel/env" relative to directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54049008/

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