2017-03-07 269 views
0

我的目录结构是这样的:在导入文件阵营本地

web/ 
    js/ 
    Test.js 
mobile 
    index.android.js 

index.android.js,我加import Test from '../web/js/Test。不过,我得到这个错误:

(node:590) UnhandledPromiseRejectionWarning: 
Unhandled promise rejection (rejection id: 12): 
UnableToResolveError: 
Unable to resolve module `../web/js/Test.js` from `/mnt/d/.../mobile/index.android.js`: 
Directory /mnt/d/.../web/js/Test.js doesn't exist 

我试图重新运行react-native start,它仍然没有工作。我没有安装Watchman,但我不认为这是问题。

+0

我不知道你在试着克去做。您是否试图在React Native中重复使用您的React网页代码? React Native是否允许您从项目根目录及其嵌套文件夹以外的任何位置导入js文件?看起来你似乎错过了你提到的导入的单引号,尽管这可能是你的错误。另一个问题是如何定义测试。您是否正确导出该组件?我们需要更多信息来确定发生了什么。 – nbkhope

回答

0

1)在移动文件夹的package.json添加作为依赖性:

"dependencies": { 
    //...your deps here + 
    "commonWebAndMobileDeps": "file:../web/js/common" 
} 

2)确保Test.js是在web/js/common

3)在移动文件夹运行npm install

4 )在移动导入它与:

import Test from 'commonWebAndMobileDeps/Test'