2

I cannot figure out how to update a package installed from a git repository.

Say I have a package at git+ssh://git@gitlab.example.com:project/my-package.git and it's already installed.

Now, if I:

  1. push into my-package master branch;
  2. run npm i or npm update;

then nothing gets updated.

I thought the version field (from the dependent package.json of my-package) might raise the problem so I removed it and re-installed the package from scratch. Unfortunately it didn't help, the package is still not being updated.

Any ideas?

Onkeltem
  • 1,889
  • 1
  • 18
  • 27
  • Does this answer your question? [npm git repository not updating versions](https://stackoverflow.com/questions/15367250/npm-git-repository-not-updating-versions) – RobC Mar 20 '20 at 12:01
  • @RobC Thanks, I saw that topic but it doesn't answer my question. The author uses commit hash which is not my case as I need my package to follow just the latest changes which is master branch I guess. – Onkeltem Mar 20 '20 at 12:45
  • Have you tried deleting the node_modules directory, then running `npm i` ? – RobC Mar 20 '20 at 12:53
  • Yes, Rob. It fetches the latest version as it should, but right after that it looses the ability to update it further: new commits of the master never get fetched again. – Onkeltem Mar 20 '20 at 12:57
  • See issue [#1727](https://github.com/npm/npm/issues/1727#issuecomment-22673783) - I think the only way currently is to re-install it again after each each commit to git. However, I'm sure this will be a feature [very soon](https://github.blog/2020-03-16-npm-is-joining-github/) ! – RobC Mar 20 '20 at 13:17
  • Yeah, who would even guess. I thought I was missing something, like version field, or maybe #branch postfix. But it has never been working! LOL. Ok, I'm gonna answer myself, explaining how I finally worked it around (which is just a copy-paste from the last comment of the thread you linked). Thanks! – Onkeltem Mar 20 '20 at 13:23

1 Answers1

6

Ok, folks. Suprisingly, it doesn't seem possible to get it working out-of-the-box.

But there is a workaround. Thanks to @RobC for pointing me out to some old (yet never resolved) issue: https://github.com/npm/npm/issues/1727

The answer is in the last comment:

https://github.com/npm/npm/issues/1727#issuecomment-354124625

Basically, to update a git package you have to re-install it directly using: npm install package-name.

Example. Say you already have your package installed and added to the dependencies like this:

{
    "dependencies": {
        "my-package": "git+ssh://git@gitlab.my.com:prj/my-package.git"
    }
}

Now, to get it updated whenever you issue npm i all you have to do is to create a postinstall script which would trigger npm i my-package:

{
    "dependencies": {
        "my-package": "git+ssh://git@gitlab.my.com:prj/my-package.git"
    },
    "scripts": {
        "postinstall": "npm update && npm i my-package"
    }
}

Now npm i will be taking more time since it's gonna run install twice. But this is what we have now.

Onkeltem
  • 1,889
  • 1
  • 18
  • 27