React Native:SyntaxError:严格模式不允许在词法嵌套语句中的函数声明
从React Native 0.22.2升级并升级一些插件后,我开始在iOS上发生这个错误。 我试过降级并重新安装一切,但我无法摆脱它。 其他人遇到这个问题:
SyntaxError: Strict mode does not allow function declarations in a lexically nested statement.
更新#1:
除了恢复模块,我已经注释了任何'use strict';
我更新的一个是npm和节点。 我正在运行节点v5.3.0和npm v3.8.3。 我不记得我有什么版本…
更新#2:
我正在使用的模块有自己的反应:
"dependencies": { "deep-freeze": "github:substack/deep-freeze", "react": "^0.14.7", "react-native": "^0.22.2", "react-native-activity-view": "^0.2.8", "react-native-animated-progress-bar": "^1.0.0", "react-native-audio": "^1.0.0", "react-native-camera": "git+https://github.com/lwansbrough/react-native-camera.git", "react-native-device-info": "^0.9.1", "react-native-fs": "^1.2.0", "react-native-html-to-pdf": "^0.1.2", "react-native-in-app-utils": "^2.3.0", "react-native-mail": "^0.2.4", "react-native-passcode-auth": "^1.0.0", "react-native-router-flux": "^2.3.13", "react-native-save-asset-library": "^1.0.0", "react-native-touch-id": "^1.2.4", "react-native-transfer": "^1.0.2", "react-native-utils": "^1.0.1", "react-native-webkit-localstorage-reader": "^1.0.0", "react-redux": "^3.1.2", "redux": "^3.0.5", "redux-thunk": "^2.0.1" }
更新#3
认为降级到React Native 0.21.0会解决这个问题,我做了以下几点:
- 删除了
node_modules
文件夹 - 将package.json的react-native版本从0.22.2更改为0.21.0,并删除了反应条目(我认为这只是0.22.2的一个要求)。
- 冉
npm install
- Ran
npm start --reset-cache
- 打开xcode并在模拟器上运行应用程序。
不幸的是,我仍然得到相同的错误。 只是为了确保它不是我的项目外部的东西,我创build了一个全新的react-native项目,它运行良好(即使反应原生0.22.2)。 所以这是与我的项目有关,但我不能为我的生活找出我做了什么导致这一点。 🙁
这个错误发生是因为babel现在在所有的模块中都加上了“use strict”。 而且它不允许在if
这样的词法块中定义一个函数
如果错误来自库,您可以尝试find哪一个导致它,并将其添加到一个.babelignore
文件。
您也可以通过使用.babelrc
文件完全禁用严格模式
{ "plugins": [ "syntax-async-functions", "syntax-class-properties", "syntax-trailing-function-commas", "transform-class-properties", "transform-es2015-arrow-functions", "transform-es2015-block-scoping", "transform-es2015-classes", "transform-es2015-computed-properties", "transform-es2015-constants", "transform-es2015-destructuring", ["transform-es2015-modules-commonjs", { "strict": false, "allowTopLevelThis": true }], "transform-es2015-parameters", "transform-es2015-shorthand-properties", "transform-es2015-spread", "transform-es2015-template-literals", "transform-flow-strip-types", "transform-object-assign", "transform-object-rest-spread", "transform-react-display-name", "transform-react-jsx", "transform-regenerator", ["transform-es2015-for-of", { "loose": true }] ] }
注意:确保清除打包程序caching( npm start -- --reset-cache
),以确保所有的转换再次运行。