How can I switch between a linked npm dependency (in development) and an installed dependency (in staging/prod)?

懵懂的女人 提交于 2019-12-10 22:48:55

问题


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.

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). Edit: 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.

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.

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?


回答1:


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.

in your development environment, use "npm link".

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.

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.

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.



来源:https://stackoverflow.com/questions/36043423/how-can-i-switch-between-a-linked-npm-dependency-in-development-and-an-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!