简体   繁体   English

如何设置在 npm package 上开发的项目

[英]how to setup a project to develop on a npm package

I have a git root project where I am using inside my root project a git subproject.我有一个 git 根项目,我在我的根项目中使用 git 子项目。 So for example I have the project MyApp and a subproject UIComponents.例如,我有项目 MyApp 和一个子项目 UIComponents。 Currently I have cloned the UIComponents repo into my project folder and added UIComponents to.gitignore of the root project.目前,我已将 UIComponents 存储库克隆到我的项目文件夹中,并将 UIComponents 添加到根项目的 .gitignore 中。 Now I want to build a npm package for UIComponents and I want to be able to switch between npm production build and development.现在我想为 UIComponents 构建一个 npm package,我希望能够在 npm 生产构建和开发之间切换。 The problem is in development the import is this:问题是在开发中,导入是这样的:

import Button from './UIComponents'

and with the npm package the import is this:并使用 npm package 导入是这样的:

import Button from '@my_name/UIComponents'

I dont want to adjust the imports everytime.我不想每次都调整进口。 The first thought that comes to mind is to develop the UIComponents inside node_modules folder but this seems not to be a nice solution.想到的第一个想法是在 node_modules 文件夹中开发 UIComponents ,但这似乎不是一个好的解决方案。

For solving this, try to use npm link .为了解决这个问题,尝试使用npm 链接 So instead of cloning it in a subdirectory that need to be added to gitignore, just check out the repository outside of you project and then link it.因此,不要将它克隆到需要添加到 gitignore 的子目录中,只需检查项目外部的存储库,然后链接它。

Example:例子:

cd ~/projects/UIComponents
npm link  
cd ~/projects/MyApp
npm link @my_name/UIComponents 

In this way you can use the same import syntax but you can develop locally in both projects at the same time without the need of publishing every change.通过这种方式,您可以使用相同的导入语法,但您可以同时在两个项目中进行本地开发,而无需发布每个更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM