Unknown option: …/.babelrc.presets

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

I am using Babel 6 for es2015 and react which requires babel-preset-es2015 and babel-preset-react.

I add the presets property in .babelrc but it throw me an error:

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 

My .babelrc file is:

{   "presets": [     "es2015",     "react"   ] } 

I can run babel src -d lib command, it works. But if I run npm start to run the babel via package.json, the error appears.

I think I can ignore the error because the app runs. But I want to know why this error and not sure what it affects.

My scripts in package.json is:

"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"   }, 

My entry.js is

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')); 

回答1:

I figured out this problem is caused by the version of babel-loader and babel-core.

In the package.json the dependencies was stated ^5.3.3 so it won't update to 6.x. Change it to >=5.3.3 or ^6.0.0.

^ means upgrade the sub version but don't upgrade main version.



回答2:

Make sure that you have those preset libraries actually in your node_modules.

I had similar but slightly different error message. The reason was that I was trying to use react preset for babel but babel-react-preset was missing from my node_modules.The end result was that babel was trying to use contents of react library as a preset.

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 


回答3:

I ran into this error when trying to build preact. Turns out I had a .babelrc file in the parent directory that was interfering; after removing it the problem went away.



回答4:

I can work with babel src --out-dir lib, but not with npm run XXX. I install Babel-cli@6.18.0 CLI globally on my machine, after I install babel-cli@ locally project, it can works with npm run.



回答5:

In some cases like mine, I have babel installed globally which throws the same error. You can uninstall the babel first then install babel-cli ^6.26.0 as part of your dependencies then try running the command npm start again



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!