ctripcorp/CRN

按照你们官方文档初始化app后添加依赖,会出现打包出错

Closed this issue · 4 comments

image

Error: Unable to resolve module `./index` from `/Users/admin/Desktop/code/CRNAPPDEMO2/.__tmp/.`: The module `./index` could not be found from `/Users/admin/Desktop/code/CRNAPPDEMO2/.__tmp/.`. Indeed, none of these files exist:
  * `/Users/admin/Desktop/code/CRNAPPDEMO2/.__tmp/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`
  * `/Users/admin/Desktop/code/CRNAPPDEMO2/.__tmp/index/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`
    at ModuleResolver.resolveDependency (/Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:163:15)
    at ResolutionRequest.resolveDependency (/Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:52:18)
    at DependencyGraph.resolveDependency (/Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/node-haste/DependencyGraph.js:283:16)
    at /Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/lib/transformHelpers.js:261:42
    at Server.<anonymous> (/Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/Server.js:1038:41)
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/Server.js:99:24)
    at _next (/Users/admin/Desktop/code/CRNAPPDEMO2/node_modules/react-native/node_modules/metro/src/Server.js:119:9)
    at <anonymous>

1、如果打开预加载,则app会崩溃(可能是因为打包失败,没有读取到jsbundle)
2、在根目录下App.js中,import一个依赖就会报上面的错误,可能是服务在拆包的过程中出错了
3、现在只在android模拟器下测试,发现这个问题,iOS还没看

我同事测试发现,通过yarn添加的包就会报上面的错误,通过npm下载的包不会,但是又回报找不到包的错误,如下:
error: bundling failed: Error: Unable to resolve module axiosfrom/Users/admin/Desktop/code/CRNAPPDEMO3/request.js: Module axiosdoes not exist in the Haste module map or in these directories: /Users/admin/Desktop/code/CRNAPPDEMO3/node_modules

1、android使用真机测试,IOS使用模拟器。
2、看错误提示找不到axios模块,本地安装完依赖后去node_moudels检查下是否存在该模块。对应axios模块使用可以参考https://www.npmjs.com/package/react-native-axios
3、可以尝试引入一些其他的模块,对比下yarnnpm安装是否也有问题。

1、android使用真机测试,IOS使用模拟器。
2、看错误提示找不到axios模块,本地安装完依赖后去node_moudels检查下是否存在该模块。对应axios模块使用可以参考https://www.npmjs.com/package/react-native-axios
3、可以尝试引入一些其他的模块,对比下yarnnpm安装是否也有问题。

通过安装其他的模块发现:
1、通过yarn添加的依赖会报错(所以必须得用npm的方式添加依赖):

[crn-cli]: node node_modules/react-native/local-cli/cli.js bundle --config rn-cli.config.js --platform ios --bundle-output common_ios.js --build-common true --entry-file crn_common_entry.js --dev false
error: unknown option `--build-common'
[crn-cli error]: Error: Command failed: node node_modules/react-native/local-cli/cli.js bundle --config rn-cli.config.js --platform ios --bundle-output common_ios.js --build-common true --entry-file crn_common_entry.js --dev false
    at checkExecSyncError (child_process.js:603:11)
    at execSync (child_process.js:640:13)
    at doBuildBundle (C:\Users\l8632\AppData\Roaming\npm\node_modules\crn-cli\commands\pack\build.js:73:9)
    at build (C:\Users\l8632\AppData\Roaming\npm\node_modules\crn-cli\commands\pack\build.js:19:5)
    at Timeout.setTimeout [as _onTimeout] (C:\Users\l8632\AppData\Roaming\npm\node_modules\crn-cli\commands\pack\localPack.js:40:4)
    at ontimeout (timers.js:427:11)
    at tryOnTimeout (timers.js:289:5)
    at listOnTimeout (timers.js:252:5)
    at Timer.processTimers (timers.js:212:10)

2、在尝试引用不同的模块后,发现crn-cli工具对部分npm模块不支持,像axios,在相同引入方式下会报找不到该模块的错误,而其他的模块不会出现这个问题

我也遇到这个问题了, 起因是安装了React-Navigation, 再打包就会报错

[crn-cli]: node node_modules/react-native/local-cli/cli.js bundle --config rn-cli.config.js --platform ios --bundle-output common_ios.js --build-common true --entry-file crn_common_entry.js --dev false
error: unknown option `--build-common'
[crn-cli error]: Error: Command failed: node node_modules/react-native/local-cli/cli.js bundle --config rn-cli.config.js --platform ios --bundle-output common_ios.js --build-common true --entry-file crn_common_entry.js --dev false
    at checkExecSyncError (child_process.js:616:11)
    at execSync (child_process.js:653:13)
    at doBuildBundle (C:\Users\34835\AppData\Roaming\npm\node_modules\crn-cli\commands\pack\build.js:73:9)
    at build (C:\Users\34835\AppData\Roaming\npm\node_modules\crn-cli\commands\pack\build.js:19:5)
    at Timeout.setTimeout [as _onTimeout] (C:\Users\34835\AppData\Roaming\npm\node_modules\crn-cli\commands\pack\localPack.js:40:4)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)