React Native:npm 链接本地依赖,无法解析模块 [英] React Native: npm link local dependency, unable to resolve module
问题描述
我正在为 React Native 开发一个按钮 ui 包.我尝试构建一个示例项目来测试此按钮.目录结构如下:
I am developing a button ui package for react native. I try to build an example project to test this button. The directory structure is as follows:
my-button/
package.json
index.js
example/
package.json
index.js
我尝试使用 npm 链接
:
cd my-button
npm link
cd example
npm link my-button
在example/node_modules/
中我可以看到my-button符号链接,VSCode也可以在my-button包中自动完成功能.
In example/node_modules/
I can see my-button symlink, VSCode also can auto complete function in my-button package.
但是执行示例应用程序会显示错误:
But execute example app will show error:
Unable to resolve module my-button ...
Module does not exist in the module map or in these directories: ...
但是错误信息中的路径是正确的.
But the path in the error message is correct.
不知道我错在哪里,或者在 React-Native 中有什么特殊的方法来处理链接本地依赖?
Don't know where I was wrong, or in React-Native have any special way to deal with link local dependency?
我也试过 npm install file:../.
.它以这种方式工作正常,但在我编辑 my-button 后,在 example/
中更新依赖项并不容易.
I also tried npm install file:../.
. It works fine in this way, but not easy to update dependency in example/
after I edited my-button.
推荐答案
npm link
命令不起作用,因为 React Native 打包程序 不支持符号链接.
The npm link
command doesn't work because React Native packager doesn't support symlinks.
经过一番研究,我发现有两种方法可以解决.
After a little research, I discovered that there are two ways to go about it.
- 在示例应用中使用 haul packager.Haul 支持符号链接,因此您可以照常使用
npm link
. - 通过
file:../
使用本地依赖,然后在node_modules
文件夹中编辑文件或每次进行更改时重新安装.
- Use haul packager in the example app. Haul supports symlinks, so you can use
npm link
as usual. - Use local dependency via
file:../
and then edit files innode_modules
folder or reinstall every time you make changes.
我发现 Haul 非常适合这个用例,甚至设置了一个 小启动项目,其中还包括 storybook,如果您有许多组件要在它们之间切换,这真的很有帮助.
I found Haul to work great for this use-case and even set-up a little starter project that also includes storybook, which is really helpful if you have many components to switch between.
这篇关于React Native:npm 链接本地依赖,无法解析模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!