Wil*_*ill 5 javascript node-modules react-native metro-bundler
我有一个现有的 React 本机应用程序。我想创建另一个应用程序并在它们之间共享一些代码。\n我利用 MetroextraNodeModules将共享代码导入到任一应用程序中。\n在任一应用程序中,我希望能够执行以下操作:
// MyApp1/src\\navigation\\app.navigator.js\nimport { someUtilModule } from \'shared\';\nRun Code Online (Sandbox Code Playgroud)\n不幸的是,当应用程序包加载到我的开发设备上时,它失败并显示:
\nerror: Error: Unable to resolve module `shared` from `src\\navigation\\app.navigator.js`: shared could not be found within the project or in these directories:\n C:\\path\\to\\my\\project\n\nIf you are sure the module exists, try these steps:\n 1. Clear watchman watches: watchman watch-del-all\n 2. Delete node_modules: rm -rf node_modules and run yarn install\n 3. Reset Metro\'s cache: yarn start --reset-cache\n 4. Remove the cache: rm -rf /tmp/metro-*\n at ModuleResolver.resolveDependency (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\node-haste\\DependencyGraph\\ModuleResolution.js:186:15)\n at ResolutionRequest.resolveDependency (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\node-haste\\DependencyGraph\\ResolutionRequest.js:52:18)\n at DependencyGraph.resolveDependency (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\node-haste\\DependencyGraph.js:287:16)\n at Object.resolve (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\lib\\transformHelpers.js:267:42)\n at C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:434:31\n at Array.map (<anonymous>)\n at resolveDependencies (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:431:18)\n at C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:275:33\n at Generator.next (<anonymous>)\n at asyncGeneratorStep (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:87:24)\nRun Code Online (Sandbox Code Playgroud)\n文件夹结构(shared文件夹存在 - 这是输出tree):
error: Error: Unable to resolve module `shared` from `src\\navigation\\app.navigator.js`: shared could not be found within the project or in these directories:\n C:\\path\\to\\my\\project\n\nIf you are sure the module exists, try these steps:\n 1. Clear watchman watches: watchman watch-del-all\n 2. Delete node_modules: rm -rf node_modules and run yarn install\n 3. Reset Metro\'s cache: yarn start --reset-cache\n 4. Remove the cache: rm -rf /tmp/metro-*\n at ModuleResolver.resolveDependency (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\node-haste\\DependencyGraph\\ModuleResolution.js:186:15)\n at ResolutionRequest.resolveDependency (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\node-haste\\DependencyGraph\\ResolutionRequest.js:52:18)\n at DependencyGraph.resolveDependency (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\node-haste\\DependencyGraph.js:287:16)\n at Object.resolve (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\lib\\transformHelpers.js:267:42)\n at C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:434:31\n at Array.map (<anonymous>)\n at resolveDependencies (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:431:18)\n at C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:275:33\n at Generator.next (<anonymous>)\n at asyncGeneratorStep (C:\\path\\to\\my\\project\\MyApp1\\node_modules\\metro\\src\\DeltaBundler\\traverseDependencies.js:87:24)\nRun Code Online (Sandbox Code Playgroud)\n我的地铁配置:
\nC:\\path\\to\\my\\project\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MyApp1\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 assets\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 components\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 navigation\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 scenes\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 services\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MyApp2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 assets\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 components\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 navigation\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 scenes\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 services\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 shared\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 assets\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 components\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 navigation\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 scenes\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 services\nRun Code Online (Sandbox Code Playgroud)\n
package.json我最终通过在以下位置创建一个虚拟文件来解决这个问题shared/:
{
"name": "shared",
"version": "0.0.1"
}
Run Code Online (Sandbox Code Playgroud)
很久以后,我删除了该文件,该项目继续运行......
现在,在诉诸虚拟之前,可能值得完全清空缓存、临时文件等package.json
| 归档时间: |
|
| 查看次数: |
2677 次 |
| 最近记录: |