首页 > 解决方案 > React Native Metro Bundler 问题与无效的左侧 AssingmentExpression

问题描述

我只是想启动 Metro bundler 来为应用程序提供服务。这是错误:

TypeError: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "StringLiteral"
at Object.validate (/Users/tyler/Code/.../node_modules/@babel/types/lib/definitions/utils.js:128:13)
at Object.validate (/Users/tyler/Code/.../node_modules/@babel/types/lib/validators/validate.js:17:9)
at NodePath._replaceWith (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/path/replacement.js:194:9)
at NodePath.replaceWith (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/path/replacement.js:178:8)
at PluginPass.MemberExpression (/Users/tyler/Code/.../node_modules/babel-plugin-transform-inline-environment-variables/lib/index.js:19:18)
at newFn (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/visitors.js:193:21)
at NodePath._call (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/path/context.js:88:12)
at TraversalContext.visitQueue (/Users/tyler/Code/.../node_modules/@babel/traverse/lib/context.js:118:16)

这意味着类似的东西Platform.OS = "ios"被转换为"ios" = "ios"就像这样<-该PR还解决了为process.env引发的TypeError,这对我来说似乎就是这种情况。我的堆栈跟踪引用...babel-plugin-transform-inline-environment-variables/lib/index.js:19:18专门处理设置 process.env。我的代码中只有两个地方使用了来自 process.env 的 var,并且都没有在分配的左侧使用。

不过,地铁捆绑器的 PR 45 早在 2017 年就被合并了。顺便说一句,我正在使用最新版本,这是在尝试使用 script 启动捆绑程序时发生的"NODE_ENV=development npm start -- --reset-cache"。请注意,我正在使用此处提到的重置缓存标志

一位同事建议该错误可能是由 react-native 插件引起的,但如果是这种情况,我还没有发现它。我们只是将这个应用程序迁移到 Babel 7,仅供参考。我现在在想法上碰壁了,谷歌的踪迹越来越陈旧。还有什么可能是导致此错误的原因?

以下是我的 package.json 中的依赖项,以防有帮助:

}
 ...
 "dependencies": {
   "@babel/runtime": "^7.0.0",
   "lodash.capitalize": "^4.2.1",
   "lodash.debounce": "^4.0.8",
   "lodash.find": "^4.6.0",
   "phoenix": "^1.3.4",
   "prettier": "^1.14.2",
   "prop-types": "^15.6.2",
   "react": "^16.4.2",
   "react-native": "0.57.0",
   "react-native-firebase": "^4.3.8",
   "react-native-keep-awake": "^3.1.0",
   "react-native-linear-gradient": "^2.4.2",
   "react-native-router-flux": "^4.0.1",
   "react-native-swiper": "^1.5.13",
   "react-native-tts": "^2.0.0",
   "react-redux": "^5.0.7",
   "redux": "^4.0.0",
   "redux-thunk": "^2.3.0",
   "spokestack-react-native": "0.0.2"
 },
 "devDependencies": {
   "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
   "@babel/plugin-transform-runtime": "^7.0.0",
   "babel-core": "^7.0.0-bridge.0",
   "babel-plugin-transform-inline-environment-variables": "^0.4.3",
   "imagemin": "^6.0.0",
   "imagemin-jpegtran": "^5.0.2",
   "imagemin-optipng": "^5.2.1",
   "imagemin-svgo": "^7.0.0",
   "jest": "^23.6.0",
   "metro-react-native-babel-preset": "^0.45.2",
   "pre-commit": "^1.2.2",
   "precise-commits": "^1.0.2",
   "react-test-renderer": "^16.4.2",
   "redux-mock-store": "^1.5.3"
  },
  ...
}

...此外,babel.config.js按照 babel 7

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    '@babel/plugin-transform-runtime',
    '@babel/plugin-proposal-object-rest-spread',
    'transform-inline-environment-variables'
  ]
}

更新:我在 react-* 包中使用 grep 来查找将 process.env 用作左手赋值的其他行。只找到了一些,但无法解决问题。

标签: react-nativebabeljs

解决方案


您在日志中看到的错误与babel-plugin-transform-inline-environment-variables. 如果您从配置文件中排除它,则构建应该通过。

我面临同样的问题,到目前为止还没有找到一个好的解决方案,所以尝试解决如何传递环境变量的方法。


推荐阅读