React Native:SyntaxError:严格模式不允许在词法嵌套语句中进行函数声明 [英] React Native: SyntaxError: Strict mode does not allow function declarations in a lexically nested statement
问题描述
从React Native 0.22.2升级并升级一些插件后,我开始在iOS上收到此错误。我已经尝试降级并重新安装一切,但我无法解决它。其他人遇到过这个:
I've started getting this error on iOS after upgrading from React Native 0.22.2 and upgrading a few plugins. I've tried downgrading and reinstalling everything but I can't get rid fix it. Anyone else encountered this:
SyntaxError: Strict mode does not allow function declarations in a lexically nested statement.
更新#1:
此外恢复我已经注释掉任何'use strict'的模块;
我更新的一件事是npm和node。我正在运行节点v5.3.0和npm v3.8.3。我不记得我有什么版本...
One of the things I updated was npm and node. I'm running node v5.3.0 and npm v3.8.3. I don't remember what versions I had...
有模块我正在使用react native:
There are the modules I'm using in react native:
"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会解决这个问题我做了以下:
Update #3
With the thought that downgrading to React Native 0.21.0 would fix this problem I did the following:
- 删除
node_modules
文件夹 - 将package.json的react-native版本从0.22.2更改为0.21.0并删除了react条目(我认为这只是0.22.2的要求)。
- Ran
npm install
- Ran
npm start --reset-cache
- 打开xcode并在模拟器上运行应用程序。
- Deleted the
node_modules
folder - Changed package.json's react-native version from 0.22.2 to 0.21.0 and removed the react entry (I think that's only a requirement for 0.22.2).
- Ran
npm install
- Ran
npm start --reset-cache
- Opened xcode and ran the app on the simulator.
不幸的是,我仍然遇到同样的错误。为了确保它不是我的项目外部的东西我创建了一个全新的反应原生项目,它运行良好(即使反应本机0.22.2)。所以这与我的项目有关,但我不能为我的生活弄清楚我做了什么导致这个。 :(
Unfortunately, I'm still getting the same error. Just to make sure it wasn't something external to my project I created a brand new react-native project and it ran fine (even with react native 0.22.2). So it's something to do with my project but I can't for the life of me figure out what I did to cause this. :(
推荐答案
发生此错误是因为babel现在将use strict添加到所有模块。并且它不允许定义函数在之类的词汇块中如果
This error happens because babel now adds 'use strict' to all modules. And it doesn't allow defining a function inside a lexical block like if
如果错误来自库,你可以尝试找出哪一个导致它并将其添加到 .babelignore
文件。
If the error comes from a library you could try to find which one cause it and add it to a .babelignore
file.
您还可以使用<$完全禁用严格模式c $ c> .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 }]
]
}
注意:make一定要清除打包缓存( npm start - --reset-cache
)以确保所有转换再次运行。
Note: make sure to clear the packager cache (npm start -- --reset-cache
) to make sure all the transforms are run again.
这篇关于React Native:SyntaxError:严格模式不允许在词法嵌套语句中进行函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!