我正在使用Babel 6 for es2015并做出反应,需要babel-preset-es2015
和babel-preset-react
。
我在presets
中添加了.babelrc
属性,但它给我一个错误:
ERROR in ./src/client/entry.js
Module build failed: ReferenceError: [BABEL] /Users/brick/Dropbox/learncoding/node.js/isomorphic/src/client/entry.js: Unknown option: /Users/brick/Dropbox/learncoding/node.js/isomorphic/.babelrc.presets
at Logger.error (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/logger.js:58:11)
at OptionManager.mergeOptions (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126:29)
at OptionManager.addConfig (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:107:10)
at OptionManager.findConfigs (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:168:35)
at OptionManager.init (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229:12)
at File.initOptions (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/index.js:147:75)
at new File (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/index.js:137:22)
at Pipeline.transform (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/pipeline.js:164:16)
at transpile (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-loader/index.js:12:22)
at Object.module.exports (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-loader/index.js:69:12)
@ multi main
我的.babelrc
文件是:
{
"presets": [
"es2015",
"react"
]
}
我可以运行babel src -d lib
命令,它有效。但是,如果我通过npm start
运行babel
来运行package.json
,则会出现错误。
我想我可以忽略错误,因为应用程序运行。但我想知道为什么会出现这种错误并且不确定它会对它产生什么影响。
scripts
中的我package.json
是:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf lib",
"build": "npm run clean && /usr/local/bin/babel src -d lib --experimental",
"server": "nodemon lib/server/server",
"dev-server": "node lib/server/webpack",
"watch-js": "/usr/local/bin/babel src -d lib --experimental -w",
"start": "npm run watch-js & npm run dev-server & npm run server"
},
我的entry.js
是
import React from "react";
import Router from "react-router";
import ReactDOM from "react-dom";
import routes from "./routes";
import DataWrapper from './DataWrapper';
import createBrowserHistory from 'history/lib/createBrowserHistory';
let history = createBrowserHistory();
var data = JSON.parse(document.querySelector('#data').innerHTML);
ReactDOM.render(<DataWrapper data={data}><Router history = {history}>{routes}</Router></DataWrapper>, document.querySelector('#app'));
答案 0 :(得分:39)
我发现此问题是由babel-loader
和babel-core
版本引起的。
在package.json
中声明了^5.3.3
依赖关系,因此它不会更新到6.x.将其更改为>=5.3.3
或^6.0.0
。
^
表示升级子版本但不升级主版本。
答案 1 :(得分:2)
确保您的node_modules
实际存在这些预设库。
我有类似但略有不同的错误消息。原因是我尝试使用react
预设为babel,但我的babel-react-preset
中缺少node_modules
。最终结果是babel正在尝试使用react
的内容库作为预设。
ERROR in ./ui/js/myproject.js
Module build failed: ReferenceError: [BABEL] /home/jsyrjala/myproject/ui/js/myproject.js: Unknown option: /home/jsyrjala/myproject/node_modules/react/react.js.Children
at Logger.error (/home/jsyrjala/myproject/node_modules/babel-core/lib/transformation/file/logger.js:43:11)
at OptionManager.mergeOptions (/home/jsyrjala/myproject/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:18)
at OptionManager.mergePresets (/home/jsyrjala/myproject/node_modules/babel-core/lib/transformation/file/options/option-manager.js:333:16)
at OptionManager.mergeOptions (/home/jsyrjala/myproject/node_modules/babel
答案 2 :(得分:2)
尝试构建preact
时遇到此错误。事实证明我在父目录中有一个.babelrc
文件正在干扰;删除后,问题就消失了。
答案 3 :(得分:1)
我可以使用babel src --out-dir lib
,但不能使用npm run XXX
。我在我的机器上全局安装Babel-cli@6.18.0 CLI,安装babel-cli @ local项目后,它可以与npm run
一起使用。
答案 4 :(得分:0)
在某些情况下,比如我的,我在全球范围内安装了babel,它会抛出相同的错误。您可以先卸载babel,然后安装babel-cli ^ 6.26.0作为依赖项的一部分,然后尝试再次运行命令npm start