node.js 错误:使用 npm install --global babel-preset-react 安装时找不到预设的“react”,但在没有全局标志的情况下工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33538403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Couldn't find preset "react" when installed using npm install --global babel-preset-react but works without global flag
提问by Karan
I installed Babel CLI (version 6) using npm install --global babel-cli. I then install react preset using npm install --global babel-preset-react.
我使用npm install --global babel-cli. 然后我使用npm install --global babel-preset-react.
I then setup the .babelrcfile in the project directory to
然后我.babelrc将项目目录中的文件设置为
{
"presets": ["react"]
}
When I try to build a JSX file it fails with
当我尝试构建一个 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)
If I install the preset without --global flag (i.e. installs in node_modules/ folder locally) then the build works. How do i set up to get babel to work with a global preset?
如果我安装没有 --global 标志的预设(即安装在本地 node_modules/ 文件夹中),那么构建工作。我如何设置让 babel 使用全局预设?
采纳答案by Petar Vorotnikov
You can specify the absolute (or relative) path to the preset you are trying to use, e.g:
您可以指定您尝试使用的预设的绝对(或相对)路径,例如:
babel --presets /usr/local/lib/node_modules/babel-preset-react --watch jsx/ --out-dir js/
回答by Tarandeep Singh
Optional Fix
可选修复
You can do it this way. Write these lines in your prompt. Now the only thing is with global you might have to use the fix suggested above by @Petar which is
你可以这样做。在您的提示中写下这些行。现在唯一的事情是使用 global 您可能必须使用@Petar 上面建议的修复程序
babel --presets /usr/local/lib/node_modules/babel-preset-react --watch jsx/ --out-dir js/
but this one does all you need.
但这一款可以满足您的所有需求。
npm i babel-cli babel-preset-react
babel --presets react jsx/ --watch --out-dir js/
and Then optionally add a .gitignore file in your github repo with content = node_modules/ now run your jsx transformation with the same command.
然后可以选择在你的 github 存储库中添加一个 .gitignore 文件,内容 = node_modules/ 现在使用相同的命令运行你的 jsx 转换。

