如何在链接的 npm 依赖项(在开发中)和已安装的依赖项(在登台/生产中)之间切换? [英] How can I switch between a linked npm dependency (in development) and an installed dependency (in staging/prod)?

查看:62
本文介绍了如何在链接的 npm 依赖项(在开发中)和已安装的依赖项(在登台/生产中)之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在处理的自定义 npm 模块,它有一个 GitHub 存储库.我也在做一个使用自定义模块的项目.在处理更大的项目时,最好使用 npm link,这样我就可以对模块进行更改并立即在主项目中看到它们.

I have a custom npm module that I am working on, and it has a GitHub repo. I'm also working on a project that uses the custom module. When working on the larger project, it is nice to use npm link so I can make changes to the module and see them right away in the main project.

为了部署到临时或生产环境,我使用了shrinkwrap 和shrinkpack 所以我可以在每次部署后执行 npm install (一些依赖项需要二进制文件,开发系统与生产系统不同,因此它们确实需要安装,而不仅仅是保持在源代码控制中). 我将其划掉,因为下面的答案在技术上解决了我的问题,即使它不能解决这一特定问题,但这并不像其他问题那么重要

To deploy to staging or production, I use shrinkwrap and shrinkpack so I can do an npm install after every deploy (some of the dependencies need binaries, and dev systems aren't the same as production systems, so they do need to be installed and not just kept in source control). I'm crossing this out as the answer below technically solves my issue, even though it doesn't solve for this particular point, but that wasn't as important as the rest of it.

当然,由于模块链接到我的主项目并且未在 package.json 中列出,因此部署和安装完全错过了它.我可以继续将它列在 package.json 中并让它指向适当的 GitHub 存储库,但是每次我需要测试主项目中的更改时,我都必须提交并推送这些更改更改,然后更新主项目,终止并重新启动应用程序……这很快就会让人厌烦.

Of course, since the module is linked to my main project and not listed in package.json, a deploy and install misses it entirely. I can go ahead and list it in package.json and have it point to the appropriate GitHub repo, but then every time I need to test a change in the main project I would have to commit and push those changes, then update the main project, kill and restart the app...that would get tiresome pretty quickly.

我想我需要一些与devDependencies"相反的东西;我可以让它在开发者上安装模块,但在登台或生产上执行npm install时从GitHub安装它.除了记住每次需要来回手动更改 package.json 之外,有没有更好的方法来做到这一点?

I guess I need something like the opposite of "devDependencies"; something where I can have it not install the module on dev, but do install it from GitHub when doing npm install on staging or production. Other than remembering to manually change package.json every time I need to go back and forth, is there a better way to do this?

推荐答案

您可以在 package.json 文件中指定一个 github 存储库作为要安装的包:

you can specify a github repository as your package to install, in your package.json file:

{
  dependencies: {
    "my-library": "githubusername/my-library"
  }
}

这将适用于您的生产环境.

this will work in your production environment.

在您的开发环境中,使用npm 链接".

in your development environment, use "npm link".

在my-library"文件夹中,直接运行npm link.这会告诉你本地机器上的 npm my-library" 可以作为链接使用.

from within the "my-library" folder, run npm link directly. that will tell npm on your local box that "my-library" is avaialable as a link.

现在,在使用my-library"的项目中,运行 npm link my-library.这将创建一个指向my-library"的本地开发版本的符号链接,允许您更改该存储库中的代码并使其在需要它的其他项目中工作.

now, in your project that uses "my-library", run npm link my-library. this will create a symlink to your local development version of "my-library", allowing you to change code in that repository and have it work in your other project that needs it.

准备好推送到生产环境后,将my-library"推送到您的 github 存储库,然后您就可以像往常一样在服务器上进行 npm install.

once you are ready to push to production, push "my-library" to your github repository, and then you can npm install on your servers, like normal.

这篇关于如何在链接的 npm 依赖项(在开发中)和已安装的依赖项(在登台/生产中)之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆