Assumed that I have written a module for Node.js which I would like to keep private. I know that I can (should) add the line:
"private": "true"
to the package.json
file, and I also know that I can npm install
this module using a file system path or a link to a git repository, including GitHub.
I also know that I can put such a file system path or a link to a git repo into package.json
, so that the dependencies
part may look somewhat like this:
"dependencies": {
"myprivatemodule": "git@github.com:..."
}
What I now want is not to link to the latest version, but to a specific one. The only possibility I know of is to link to a specific commit using its ID. But this is way less readable and worse maintainable than using a version number such as 0.3.1
.
So my question is: Is it possible to specify such a version number anyway and make npm search the git repository for the latest commit that includes this version?
If not, how do you resolve this issue in your projects? Do you live with commit IDs or is there a better solution to this?
If by version you mean a tag or a release, then github provides download links for those. For example, if I want to install fetch version 0.3.2 (it is not available on npm), then I add to my
package.json
underdependencies
:The only disadvantage when compared with the commit hash approach is that a hash is guaranteed not to represent changed code, whereas a tag could be replaced. Thankfully this rarely happens.
Update:
These days the approach I use is the compact notation for a GitHub served dependency:
Where commit can be anything commitish, like a tag. In the case of GitHub you can even drop the initial
github:
since it's the default.This command installs npm package
username/package
from specific git commit:Here
3d0a21cc
is first 8 characters of commit hash.My example comment to @qubyte above got chopped, so here's something that's easier to read...
The method @surjikal described above works for branch commits, but it didn't work for a tree commit I was trying include.
The archive mode also works for commits. For example, fetch @ a2fbf83
npm:
yarn:
format:
Here's the tree commit that required the
/archive/
mode:for the related vuex commit
If you're doing this with more than one module and want to have more control over versions, you should look into having your own private npm registry.
This way you can npm publish your modules to your private npm registry and use package.json entries the same way you would for public modules.
https://docs.npmjs.com/files/package.json#dependencies
A dependency has to be available from the
registry
to be installed just by specifying aversion
descriptor.You can certainly create and use your own registry instead of
registry.npmjs.org
if your projects shouldn't be shared publicly.But, if it's not in a registry, it'll have to be referenced by URL or Git URL. To specify a version with a Git URL, include an appropriate
<commit-ish>
, such as a tag, at the end as a URL fragment.Example, for a tag named
0.3.1
:Depending on your OS, you may also be able to
link
to the dependency in another folder where you have it cloned from Github.The accepted answer did not work for me. Here's what I'm doing to pull a package from github:
Or adding it manually on package.json: