react native init: ReferenceError: [BABEL] Unknown option: Unknown option: base.optional

拥有回忆 提交于 2019-12-22 14:16:28

问题


I'm getting the following error upon running react-native init testNative

ReferenceError: [BABEL] /Users/m/git/testNative/node_modules/react-native/local-cli/bundle/bundle.js: Unknown option: base.optional
    at Logger.error (/Users/m/git/testNative/node_modules/babel-core/lib/transformation/file/logger.js:41:11)
    at OptionManager.mergeOptions (/Users/m/git/testNative/node_modules/babel-core/lib/transformation/file/options/option-manager.js:289:18)
    at OptionManager.init (/Users/m/git/testNative/node_modules/babel-core/lib/transformation/file/options/option-manager.js:486:10)
    at File.initOptions (/Users/m/git/testNative/node_modules/babel-core/lib/transformation/file/index.js:211:75)
    at new File (/Users/m/git/testNative/node_modules/babel-core/lib/transformation/file/index.js:129:22)
    at Pipeline.transform (/Users/m/git/testNative/node_modules/babel-core/lib/transformation/pipeline.js:48:16)
    at Object.transformFileSync (/Users/m/git/testNative/node_modules/babel-core/lib/api/node.js:118:10)
    at compile (/Users/m/git/testNative/node_modules/babel-register/lib/node.js:100:20)
    at loader (/Users/m/git/testNative/node_modules/babel-register/lib/node.js:128:14)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/m/git/testNative/node_modules/babel-register/lib/node.js:138:7)

I've tried the advice in the following posts (https://github.com/facebook/react-native/issues/5, https://github.com/babel/babel-loader/issues/132, https://github.com/babel/babelify/issues/129, React 0.14 error: Module build failed: ReferenceError: [BABEL] .../node_modules/eslint-loader/index.js!/.../main.jsx: Unknown option: base.stage), but with no avail.

My current hypothesis is that because I don't have a .babelrc file anywhere on my system, it's falling back to "base.optional." At least adding a .babelrc file to my project folder is the only thing that yielded a different outcome (downgrading node, babel, or react-native did nothing). So, if this is the solution, does anyone know what my .babelrc file should include for a react-native project?

Thanks


回答1:


Here is one of the .babelrc files that you can use with your RN project. Note that RN works fine with no .babelrc file in your project (it falls back to node_modules/react-native/.babelrc file.

{
  "retainLines": true,
  "compact": true,
  "comments": false,
  "plugins": [],
  "presets": ["stage-0", "react", "react-native"],
  "sourceMaps": false,
}

You also need to add these line to your package.json file and install the dev-dependencies : npm i --save-dev <package-name>

"devDependencies": {
  "babel-preset-react": "^6.3.13",
  "babel-preset-react-native": "^1.4.0",
  "babel-preset-stage-0": "^6.3.13"
}


来源:https://stackoverflow.com/questions/35692139/react-native-init-referenceerror-babel-unknown-option-unknown-option-base

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