Repository navigation
Missing dependencies after running npm install a second time #16839
Description
I'm opening this issue because:
- npm is crashing.
- npm is producing an incorrect install.
- npm is doing something I don't understand.
- Other (see below for feature requests):
What's going wrong?
When there is an existing lockfile, npm5 does not install all necessary packages. (This issue specifically affects any project that depends on foundry-kue-scheduler)
The initial invocation of npm install installs the correct set of dependencies, and creates a lockfile as I would expect.
When I delete node_modules, and run npm install again (against the same lockfile), I get a different set of packages (several transitive dependencies are missing), and npm alters the contents package-lock.json.
How can the CLI team reproduce the problem?
This seems to be a minimal test-case that shows how npm install is broken for any package that depends on foundry-kue-scheduler:
# scaffold a minimal project
$ mkdir npm5-test
$ cd npm5-test
$ npm init -y
# install/save my first/only dependency
$ npm i foundry-kue-scheduler
npm WARN deprecated redlock@2.1.1: possible critical bug, see https://github.com/mike-marcacci/node-redlock/issues/31
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN npm5-test@1.0.0 No description
npm WARN npm5-test@1.0.0 No repository field.
added 203 packages in 11.601s
# npm installed my packages and creates a lockfile
$ ls -lh
total 88
drwxr-xr-x 194 andrew staff 6.4K May 31 15:09 node_modules
-rw-r--r-- 1 andrew staff 38K May 31 15:09 package-lock.json
-rw-r--r-- 1 andrew staff 286B May 31 15:09 package.json
# one of my transitive dependencies is present
$ ls -lh node_modules/kue/package.json
-rw-r--r-- 1 andrew staff 2.0K May 31 15:09 node_modules/kue/package.json
# At this point, everything is OK.
# NPM has installed the set of packages that I expected.
# I can run `npm install` again, and it correctly reports that there's nothing to do.
# now, let's remove node_modules and reinstall (as though I'm installing
# packages for the first time in a repo that has package-lock.json checked-in,
# and node_modules is listed in .gitignore )
$ rm -rf node_modules/
$ npm i
npm WARN npm5-test@1.0.0 No description
npm WARN npm5-test@1.0.0 No repository field.
added 192 packages in 4.73s
# note that fewer packages were installed, and npm MODIFIED the extant lockfile
$ ls -lh
total 88
drwxr-xr-x 185 andrew staff 6.1K May 31 15:11 node_modules
-rw-r--r-- 1 andrew staff 36K May 31 15:11 package-lock.json
-rw-r--r-- 1 andrew staff 286B May 31 15:11 package.json
# also, my transitive dependency is gone.
$ ls node_modules/kue/package.json
ls: node_modules/kue/package.json: No such file or directory
# subsequent invocations of npm install make things even worse
$ npm i
npm WARN npm5-test@1.0.0 No description
npm WARN npm5-test@1.0.0 No repository field.
removed 190 packages in 3.026s
# ahhhhhh!
$ ls -lh
total 16
drwxr-xr-x 5 andrew staff 170B May 31 15:15 node_modules
-rw-r--r-- 1 andrew staff 503B May 31 15:15 package-lock.json
-rw-r--r-- 1 andrew staff 286B May 31 15:15 package.jsonHere's a gist that shows the package structure, as well as the changes to package-lock.json between the first and second invocation of npm install.
supporting information:
npm -vprints:5.0.0node -vprints:v6.10.3npm config get registryprints:- Windows, OS X/macOS, or Linux?: macOS (also verified inside Docker w/ Linux)
- Network issues:
- Geographic location where npm was run:
- I use a proxy to connect to the npm registry.
- I use a proxy to connect to the web.
- I use a proxy when downloading Git repos.
- I access the npm registry via a VPN
- I don't use a proxy, but have limited or unreliable internet access.
- Container:
- I develop using Vagrant on Windows.
- I develop using Vagrant on OS X or Linux.
- I develop / deploy using Docker.
- I deploy to a PaaS (Triton, Heroku).
Activity
schmod commentedon May 31, 2017
Some quick discussion:
The broken transitive dependency (
kue) referenced in the reproduction steps above was resolved through a GitHub dependency (foundry-kue-schedulerdepends on a GitHub-hosted fork ofkue-scheduler). I haven't been able to identify if that's the cause of this regression, but it seems like a potential culprit.The fact that the "bare"
npm installcommand could modify an existingpackage-lock.jsonwas surprising to me.I would not be surprised if
npm prune,npm update, ornpm install <package>modify the lockfile, but definitely was not expectingnpm installto make changes on its own.This seems like a bug, but there doesn't seem to be much documentation about the contracts that NPM makes with regards to lockfiles, or how they're meant to be used in a typical workflow. For all I know, this could be an expected behavior. More documentation would be helpful.
My $0.02 is that the "bare"
npm installcommand should strive to be idempotent, and should not make any changes (outside ofnode_modulesor caches) when a lockfile is present.schmod commentedon May 31, 2017
Dropping a link to #16837 here, because it seems similar.
kelsin commentedon May 31, 2017
Seeing these same results. We committed our package-lock.json after a brand new
npm iand then on Jenkins we (running same node version, same npm version) see missing module errors. I also see the same results where the package-lock.json gets weird and smaller after everynpm irun.By weird, I'm seeing an issue where the repository URL ends up in the "version" field of package-lock.json.
kelsin commentedon May 31, 2017
And to confirm I also see this only when locally removing the node_modules folder (which is also the environment in CI)
ericeslinger commentedon May 31, 2017
I am seeing something very similar when updating a single package. For example, once everything is working fine, I can do this:
note that I just deleted one package that I directly depend on, and then running
npm ia second time correctly installs the deleted package, but seems to delete the packages that were depended on by that package I manually deleted. So now (in this case),plumpis properly installed, butrxjsandmerge-optionsare both missing (both are in the dependencies entry for plump). I'm not entirely sure where the 4 removed packages total comes from - I assume they aresymbol-observableandis-plain-obj, which are the sole declared dependencies ofrxjsandmerge-optionsrespectively.What is weirder is that if I do
npm i --no-save rxjs, I get back "added 2 packages", but they don't actually add (npm ls rxjsshows it's not there, and just doing anls -laR node_modules | grep rxjsconfirms this).If I do something silly like go into a working directory that's all properly installed and do
npm i --no-save rxjs(or some other package that is a declared dependency of the thing I'm working on but was already installed), it goes nuts - in this case it removed 123 packages and updated 211 packages, when in fact it should have done nothing (rxjs was already installed and up to date).In all cases, this is only repaired by deleting both package-lock.json and node_modules and doing a fresh
npm i.Add link to npm/npm#16839
mbland commentedon Jun 2, 2017
I was going to file my own bug, but I think this one covers my experience. I've managed to create a minimal experiment and demo repo that reproduces this behavior; the README has all the details:
https://github.com/mbland/npm-v5-package-lock-bug-demo
Hopefully it's helpful here. The upshot: the problem appears related to
devDependencypackages whose owndependencyspecs are set tolatest(at least, that's how it appeared in my project with adevDependencyonlive-server). It also only appears to manifest when the originalnode_modulesdirectory is removed and recreated.zkat commentedon Jun 2, 2017
@mbland awesome work! Thanks for this 🎉
mbland commentedon Jun 4, 2017
FYI, just re-ran all the scenarios with npm v5.0.2, and got essentially the same results. The only difference is that buffer-shims is no longer installed, because readable-stream v2.2.10 no longer depends on it.
Glavin001 commentedon Jun 5, 2017
I think I was able to work around it by manually installing the missing dependencies until there were none left and letting
npmupdate the package-lock.json as I go along.For example:
And
npmupdated bothpackage.jsonandpackage-lock.json.Now to confirm it actually helped anything:
Looking good!
I'm going to do some more testing, however I wanted to report my strange findings here in case it helps anyone. Hope we can get this resolved soon!
cwilsn commentedon Jun 5, 2017
I can confirm the comment by @Glavin001. We handled it by having our developers that removed node_modules remove
package-lock.jsonbefore runningnpm installand as long as npm producedpackage-lock.jsonrather than pulling from a git repo subsequentnpm installcommands did not modify the lock file.Remove `package-lock.json`
Babazon commentedon Jun 8, 2017
I upgraded to npm5 while using Expo (for react native, it installs about 700 packages within node_modules without adding them to package.json)
I did npm install for the first time to add one package and it pruned the entire node_modules directory, and I had to downgrade to npm4.x , create a new Expo project, and copy paste my src folder and configs.
Is it a feature that npm5 prunes by default on each install or was it an attempt to make it "better than yarn"? Because that didn't work at all, I went back to yarn after all.
116 remaining items
evenfrost commentedon Mar 10, 2018
Same here on npm v5.7.1 with cyrillic-to-translit-js being defined in package.json as
Seems the issue is still relevant. @zkat mind reopening?
SudoPlz commentedon Mar 21, 2018
This issue happened to me when I requested from the Development server a bundle without the platform and dev flags.
I was requesting the bundle like
http://192.168.192.30:8081/index.bundleOnce I changed it to
http://192.168.192.30:8081/index.bundle?platform=ios&dev=trueit worked well again.greysteil commentedon Mar 29, 2018
I've just seen this on a Dependabot pull request and dug into it. I think I have a fix, but my knowledge of the npm codebase / JS isn't good enough to implement it and (particularly) write tests for it.
First up, here are some reproduction steps:
The
--package-lock-onlymodifier isn't necessary in the above (it just speeds things up), and the argumentnpm@5.8.0could be anything - the problem is caused by passing an argumentNext up, I think I've isolated the problem in npm:
The cause of the above is here. The top part of the conditional is only run if an argument is passed, but the problem is that these lines are only run if an argument isn't passed. For whatever reason, git dependencies don't make it into the tree unless they're loaded.
Finally here is a PR that I'm pretty sure fixes this
I've put the above together into a PR at #20198, but JavaScript isn't my strongest language and it could really do with a test. There might also be performance optimisations available that I'm not thinking about (and were the reason for the conditional to start with, although this seems unlikely given the codepath).
I'd love help from a maintainer to get it tested and over the line.
install/deps: Let git deps w/ lock only match package.json
user3323 commentedon Apr 23, 2018
The same problem!
What is the need for
npm install somethingbeing so destructive ?user3323 commentedon Apr 25, 2018
Well, I've found a reason of this "remove all of them" behavior of npm.
It's all about
package.jsonfile anddependencies/devDependenciesfields.In the process of installation of ANY package locally, npm will auto-scan versions of every package recorded in the
package.jsonfile, and will compare all of them with versions of packages that you have innode_modulesfolder. And, in case when you take someone's app (withpackage.jsonfile, where recorded versions ^4.x.x) and leave yournode_modulesfolder with newer (versions 5.x.x) packages - npm will delete all the packages, that didn't match one with other.I will just remind, that version
^4.x.xdoes not match with5.x.x, because this triangle caret^takes "upper effect" only on minor and patch numbers of version. (https://bytearcher.com/articles/semver-explained-why-theres-a-caret-in-my-package-json/)So, I think it can be confusing for a lot of people. When, for example they know, that Angular 5 doesn't has "cataclysmic" changes in compare to Angular 4.
But "wise" npm thinks otherwise.
And if your
package.jsonlooks like this:... and all your
node_modules/@angular/packages have versions5.x.x- all of them will be removed by npm. Furthermore - npm will remove all other packages, that are bound with/@angular/packages. Thus, ifnode_modulesfolder has ~800 subfolders, when you replacepackage.jsonwith lower versions, and simply install, for example, bootstrap (npm install bootstrap) - there will be ~150 subfolders in node_modules folder, already. Yep, minus 650.It was like that couple days ago with npm v5.6.0 ... and yesterday they released version 6.0.0, and it still works in this not very smart order.
So, be careful, folks! 😎
fnlctrl commentedon May 24, 2018
git+ssh dependencies are still removed in v6.1.0....