React Native:SyntaxError:严格模式不允许在词法嵌套语句中进行函数声明

时间:2016-03-26 02:13:32

标签: ios react-native

从React Native 0.22.2升级并升级一些插件后,我开始在iOS上收到此错误。我已经尝试降级并重新安装一切,但我无法解决它。其他人遇到过这个:

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。我不记得我有什么版本......

更新#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可以解决这个问题,我做了以下几点:

  1. 删除了node_modules文件夹
  2. 将package.json的react-native版本从0.22.2更改为0.21.0并删除了react条目(我认为这只是0.22.2的要求)。
  3. npm install
  4. npm start --reset-cache
  5. 打开xcode并在模拟器上运行应用程序。
  6. 不幸的是,我仍然遇到同样的错误。为了确保它不是我的项目外部的东西我创建了一个全新的反应本机项目,它运行良好(即使反应本机0.22.2)。所以这与我的项目有关,但我不能为我的生活弄清楚我做了什么导致这个。 :(

1 个答案:

答案 0 :(得分:0)

发生此错误是因为babel现在为所有模块添加了'use strict'。并且它不允许在像if

这样的词块中定义函数

如果错误来自某个库,您可以尝试查找哪个错误并将其添加到.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 }]
  ]
}

注意:确保清除打包缓存(npm start -- --reset-cache)以确保再次运行所有转换。