npmpackage.jsonpackage-lock.json

package.json and package-lock.json does not reflect node_module's versions


I updated my Angular project with npm and for some reason package.json and package-lock.json was not updated accordingly. So the question is, do I have to update them manually, or is there a way to update these files according to the actual versions in node_modules?

I tried npm init but that gives me a very big package.json containing every package in node_modules. Therefore I would prefer something that updates each entry in package.json according to what is actually installed.


Solution

  • I had a similar problem, where not all of the needed modules in node_modules were "required" (directly or indirectly) by package.json, so every time I installed something using npm install, it also removed a whole bunch of modules.

    My solution wasn't a very satisfactory one, but it did the trick:

    1. Renamed my package-lock.json file and package.json files to keep them safe but out of the way.
    2. Used npm init as you mentioned, to create a version of package.json that contained all of the modules in node_modules.
    3. Moved that out of the way but kept it for reference.
    4. Renamed node_modules to something else to keep it safe but out of the way.
    5. Created a brand new package.json file, again using npm init, but which had no dependencies because there was now no node_modules directory.
    6. Went through my source files finding every dependency require statement, and did an npm --save install package@version on each. I got the package name from the require and then found it in the complete package.json (from steps 2 and 3) and used the version number from that to ensure it matched what was there before. (I've got some legacy code with out-of-date modules, which is why I wanted specific versions.)
    7. Now, my package.json file is minimal, but when I run npm install, everything is up to date.