react-native 打离线包报错 child_process.js:511 throw err

本文详细描述了在使用react-native进行iOS应用开发时遇到的权限错误问题,并提供了一个简单有效的解决方案:通过修改文件权限来解决运行时的EACCES错误。

react-native run-ios 出错,信息如下

child_process.js:511
    throw err;
    ^

Error: spawnSync /Users/Lumend/Public/app/node_modules/react-native/local-cli/setup_env.sh EACCES
解决方案    chmod -R 777 node_modules
D:\AwesomeProject> npx react-native start --reset-cache warn Package stream-browserify has been ignored because it contains invalid configuration. Reason: Cannot find module 'stream-browserify\package.json' Require stack: - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\releaseChecker\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveReactNativePath.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js warn Package dns.js has been ignored because it contains invalid configuration. Reason: Cannot find module 'dns.js\package.json' Require stack: - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\releaseChecker\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveReactNativePath.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js warn Package react-native-level-fs has been ignored because it contains invalid configuration. Reason: Cannot find module 'react-native-level-fs\package.json' Require stack: - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\releaseChecker\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveReactNativePath.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js warn Package react-native-tcp has been ignored because it contains invalid configuration. Reason: Cannot find module 'react-native-tcp\package.json' Require stack: - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\releaseChecker\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveReactNativePath.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js warn Package react-native-os has been ignored because it contains invalid configuration. Reason: Cannot find module 'react-native-os\package.json' Require stack: - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\releaseChecker\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\node_modules\@react-native-community\cli-tools\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveNodeModuleDir.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\resolveReactNativePath.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js error Cannot find module 'node-libs-react-native' Require stack: - D:\AwesomeProject\metro.config.js - D:\AwesomeProject\node_modules\import-fresh\index.js - D:\AwesomeProject\node_modules\cosmiconfig\dist\loaders.js - D:\AwesomeProject\node_modules\cosmiconfig\dist\createExplorer.js - D:\AwesomeProject\node_modules\cosmiconfig\dist\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\readConfigFromDisk.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js. Error: Cannot find module 'node-libs-react-native' Require stack: - D:\AwesomeProject\metro.config.js - D:\AwesomeProject\node_modules\import-fresh\index.js - D:\AwesomeProject\node_modules\cosmiconfig\dist\loaders.js - D:\AwesomeProject\node_modules\cosmiconfig\dist\createExplorer.js - D:\AwesomeProject\node_modules\cosmiconfig\dist\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\readConfigFromDisk.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\tools\config\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\install\install.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\commands\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js - D:\AwesomeProject\node_modules\@react-native-community\cli\build\bin.js at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15) at Module._load (node:internal/modules/cjs/loader:981:27) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:177:18) at Object.<anonymous> (D:\AwesomeProject\metro.config.js:21:29) at Module._compile (node:internal/modules/cjs/loader:1364:14) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at Module.require (node:internal/modules/cjs/loader:1231:19) info Run CLI with --verbose flag for more details.
最新发布
08-15
你遇到的错误是: ``` Error: Cannot find module 'node-libs-react-native' ``` 并且在启动时有多个关于模块找不到的警告,如: ``` warn Package stream-browserify has been ignored because it contains invalid configuration. warn Package dns.js has been ignored because it contains invalid configuration. ``` --- ## 🚨 错误原因 这些错误的根本原因是你 **没有正确安装 `node-libs-react-native` 和相关依赖**,或者安装路径、版本有问题。 --- ## ✅ 解决方案 ### ✅ 步骤 1:确保安装了 `node-libs-react-native` ```bash npm install --save node-libs-react-native ``` 或使用 yarn: ```bash yarn add node-libs-react-native ``` --- ### ✅ 步骤 2:安装其他需要的 polyfill 模块 ```bash npm install --save buffer events stream process ``` 或 yarn: ```bash yarn add buffer events stream process ``` --- ### ✅ 步骤 3:运行 `rn-nodeify` 初始化 Node.js 模块模拟 ```bash npx rn-nodeify --install buffer,events,stream,dns,fs,net,process,tls,os --hack ``` > 这会自动创建 `shim.js` 文件,并在 `metro.config.js` 中引用。 --- ### ✅ 步骤 4:确保 `index.js` 中引入全局模拟模块 在你的 `index.js` 文件顶部添加: ```js import 'node-libs-react-native/globals'; ``` --- ### ✅ 步骤 5:修改 `metro.config.js`(确保如下内容) ```js const { getDefaultConfig } = require("metro-config"); const nodeLibsReactNative = require('node-libs-react-native'); module.exports = (async () => { const { resolver: { sourceExts, assetExts } } = await getDefaultConfig(); return { transformer: { getTransformOptions: async () => ({ transform: { experimentalImportSupport: false, inlineRequires: false, }, }), }, resolver: { extraNodeModules: nodeLibsReactNative, sourceExts: [...sourceExts, "js", "jsx", "json"], assetExts: assetExts.filter(ext => ext !== "svg") } }; })(); ``` --- ### ✅ 步骤 6:清理缓存并重新启动 ```bash npx react-native start --reset-cache npx react-native run-android ``` 或 iOS: ```bash npx react-native run-ios ``` --- ## ✅ 成功后效果 - ✅ 不再报错 `Cannot find module 'node-libs-react-native'` - ✅ 原始的 `dns.js`, `stream-browserify` 等警告消失 - ✅ `mqtt` 库可以在 React Native 中正常运行 --- ## 🚨 注意事项 - 确保你使用的是 React Native CLI 项目(非 Expo) - 如果你使用的是 Expo,建议改用 WebSocket + `mqttws31` 库(见相关问题) - 如果你使用了 `TypeScript`,还需要额外配置 `tsconfig.json` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值