反应原生init:ReferenceError:[BABEL]未知选项:未知选项:base.optional

时间:2016-02-29 04:45:25

标签: react-native babeljs

运行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)

我在以下帖子中尝试了这些建议(recommendedhttps://github.com/facebook/react-native/issues/5https://github.com/babel/babel-loader/issues/132https://github.com/babel/babelify/issues/129),但没有用。

我目前的假设是,因为我的系统中没有任何.babelrc文件,所以它会回到“base.optional”。至少将.babelrc文件添加到我的项目文件夹是唯一产生不同结果的东西(降级节点,babel或react-native什么都不做)。那么,如果这是解决方案,有人知道我的.babelrc文件应该包含什么反应原生项目吗?

由于

1 个答案:

答案 0 :(得分:1)

以下是您可以与RN项目一起使用的.babelrc文件之一。 请注意,RN在项目中没有.babelrc文件时工作正常(它会回退到node_modules/react-native/.babelrc文件。

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

您还需要将这些行添加到package.json文件并安装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"
}