错误:使用npm install安装时无法找到预设"react"--global babel-preset-react但是没有全局标志

Kar*_*ran 15 node.js babeljs

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

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

{
    "presets": ["react"]
}
Run Code Online (Sandbox Code Playgroud)

当我尝试构建一个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)
Run Code Online (Sandbox Code Playgroud)

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

小智 13

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

babel --presets /usr/local/lib/node_modules/babel-preset-react --watch jsx/ --out-dir js/
Run Code Online (Sandbox Code Playgroud)

  • 我正在使用咕噜声为我工作.现在转换就像:`['babelify',{presets:[__ dirname +'/ node_modules/babel-preset-es2015'],}]` (6认同)
  • 重新创建虚拟机并使用“sudo apt-get -y install nodejs-legacy npm”、“sudo npm install --global babel-cli”和“sudo npm install --global babel-preset-react”重新安装库后。该解决方案有效。**请注意,您必须删除 .babelrc 文件才能使其工作。** @Petar - 您可以编辑您的答案吗? (2认同)