运行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)
我在以下帖子中尝试了这些建议(recommended,https://github.com/facebook/react-native/issues/5,https://github.com/babel/babel-loader/issues/132,https://github.com/babel/babelify/issues/129),但没有用。
我目前的假设是,因为我的系统中没有任何.babelrc文件,所以它会回到“base.optional”。至少将.babelrc文件添加到我的项目文件夹是唯一产生不同结果的东西(降级节点,babel或react-native什么都不做)。那么,如果这是解决方案,有人知道我的.babelrc文件应该包含什么反应原生项目吗?
由于
答案 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"
}