Installing specific version of private Node.js module from Git

Hey everyone, I’m working on a private Node.js module and I’m trying to figure out the best way to manage its versions when installing from Git. I know I can use npm install with a Git repo link, but I want to target a specific version instead of always getting the latest.

I’ve set up my package.json like this:

{
  "dependencies": {
    "mySecretModule": "[email protected]:myuser/myrepo.git"
  },
  "private": true
}

But this always pulls the newest version. I could use a commit hash, but that’s not very readable or easy to maintain. Is there a way to use version numbers (like 1.2.3) with Git repos in npm? How do you handle this in your projects?

I’m looking for a solution that’s both practical and easy to manage in the long run. Any tips or best practices would be super helpful!

Another approach worth considering is using npm’s ability to reference specific branches or tags in Git repositories. You can modify your package.json to include the desired version like this:

{
  "dependencies": {
    "mySecretModule": "git+ssh://[email protected]:myuser/myrepo.git#v1.2.3"
  }
}

This method combines the benefits of version control with npm’s package management. It allows you to specify exact versions, making your builds more reproducible. Remember to create appropriate tags in your Git repository for each release. This strategy has worked well in my projects, providing a balance between flexibility and version control.

hey there! have you tried using npm’s version ranges? you can do something like this:

“mySecretModule”: “git+ssh://[email protected]:myuser/myrepo.git#semver:^1.2.3”

this way npm will grab the highest version that matches the range. its pretty neat and saves alot of headaches. hope this helps!

I’ve faced this challenge a few times and found a practical solution by using Git tags. In the module repository, I assign a tag for each release, such as v1.2.3, and then update the package.json dependency by appending the tag after a hash to the Git URL. For example, instead of a commit hash, you use [email protected]:myuser/myrepo.git#v1.2.3. This method allows you to utilize semantic versioning, making it easier to track updates and maintain consistency across projects. It also simplifies updating the version, as you only need to change the tag and reinstall the module.