gpt4 book ai didi

node.js - 错误 : Couldn't find preset "react" when installed using npm install --global babel-preset-react but works without global flag

转载 作者:IT老高 更新时间:2023-10-28 23:03:09 31 4
gpt4 key购买 nike

我使用 npm install --global babel-cli 安装了 Babel CLI(版本 6)。然后我使用 npm install --global babel-preset-react 安装 react 预设。

然后我将项目目录中的 .babelrc 文件设置为

{
"presets": ["react"]
}

当我尝试构建 JSX 文件时,它失败了

Error: Couldn't find preset "react"
at OptionManager.mergePresets (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:310:17)
at OptionManager.mergeOptions (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:12)
at OptionManager.addConfig (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:206:10)
at OptionManager.findConfigs (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:347:16)
at OptionManager.init (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:392:12)
at File.initOptions (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/index.js:191:75)
at new File (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/index.js:122:22)
at Pipeline.transform (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/pipeline.js:42:16)
at transform (/usr/local/lib/node_modules/babel-cli/lib/babel/util.js:53:22)
at Object.compile (/usr/local/lib/node_modules/babel-cli/lib/babel/util.js:62:12)

如果我在没有 --global 标志的情况下安装预设(即安装在本地的 node_modules/文件夹中),则构建工作。如何设置让 babel 使用全局预设?

最佳答案

您可以指定您尝试使用的预设的绝对(或相对)路径,例如:

babel --presets /usr/local/lib/node_modules/babel-preset-react --watch jsx/ --out-dir js/

关于node.js - 错误 : Couldn't find preset "react" when installed using npm install --global babel-preset-react but works without global flag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538403/

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