Developing an npm package and a project that depends on it at the same time

≡放荡痞女 提交于 2021-02-08 08:20:28

问题


The setup/workflow:

Project A relies on a custom npm package in package.json.

As engineers work on Project A, they will inevitably be making changes to the custom npm package as well. This is streamlined by having them clone down both the project and custom package repos, then applying npm link.

(source: http://justjs.com/posts/npm-link-developing-your-own-npm-modules-without-tears)

The question:

Assume a developer has now finished making changes to both Project A and the custom npm package. Here are the next steps:

  1. He or she must submit a Pull Request for the custom package and wait for it to be code reviewed and merged.

  2. He or she must now open Pull Request for Project A, containing a bumped version number on the custom package in package.json.

This feels clunky and prone to blocking our other developers.

Does anyone have suggestions for a better workflow when building a custom npm package and a project that depends on it at the same time?


回答1:


So you have project A depending on package B. First, if B is under rapid development, it does not make much sense to make A dependent on B (by listing it in package.json and 'npm install'-ing it): it'll bring you more pain than gain. Instead of this, copy B directly into the A and use it directly (without using any npm machinery).

Only when B's API gets stable enough, publish it as a separate module and depend on it npm way.

But this is not all! To keep things reasonably separated and decoupled, you should use git submodules.

https://git-scm.com/book/en/v2/Git-Tools-Submodules

This great feature of git allows you to put one git repo inside another git repo. This way, both of your projects are reasonably well decoupled; also this makes the process of publishing B as a separate unit much easier.

I've tried both approaches ( a) npm with git branch dependency vs. b) git submodules, no npm) on two separate middle-sized projects and I enjoyed the submodules way much more :)




回答2:


What I do is use username/repo#branch as the version in package.json. This causes npm to pull directly from my fork on GitHub, bypassing the npm registry. I tend to only do this when I cannot wait for the maintainer to publish a release to the npm registry (not that often for me).

When the maintainer publishes a release to npm then I update the version number to the new release. Also, I would not include the version number in a pull request, I would always let the maintainer(s) decide how to bump the version.




回答3:


I think one possible solution is to be more generic with the versioning in Project A's package.json.

Instead of explicitly maintaining the version number of the custom npm module dependency, we can use asterisks in the semvar statements.

Eg:

"@org/custom-node-module": "0.1.2" - requires manually changing everytime the npm package is updated.

"@org/custom-node-module": "0.*.*" - running npm install will always grab the most recent non-breaking version.



来源:https://stackoverflow.com/questions/34499753/developing-an-npm-package-and-a-project-that-depends-on-it-at-the-same-time

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