I just got a project that someone else has been working on. When I copied it I have to copy the node_modules folder or it doesn't work. Is there a way to have the package.json
be updated based on whats in the node_modules
directory?

- 5,490
- 1
- 25
- 35

- 65
- 2
- 8
-
1`node_modules` packages are provided thanks to `package.json`. You should remove `node_modules` folder, then re-run `npm install` / `yarn`. – zronn Jul 15 '19 at 15:02
-
1Sounds completely backwards. – charlietfl Jul 15 '19 at 15:04
-
It is super backwards. When i run npm install after deleting node_modules the code doesn't run anymore. Thats the problem i'm having. I think the modules have been copied and pasted too many times, and the package.json was never updated – levif1 Jul 15 '19 at 15:14
3 Answers
These two steps are backwards. node_modules/
should not be checked into Git or shared from one computer to another. It's just a representation of what's in package.json
. That file, not the directory, is the source of truth for your JavaScript dependencies. If that's not the case on your project, you'll have to fix that first to do any work on it.
First, remove node_modules
:
$ rm -rf node_modules
Or, per rcdmk's answer, rename it so you have a reference of what was there:
$ mv node_modules node_modules_backup
Then, recreate it from package.json
with your CLI tool of choice:
$ npm install # npm
$ yarn install # yarn
Finally, work through any issues in the code caused by the new dependencies. These will show as errors up in your server log, browser console, or build output. If you created a node_modules_backup
, compare the subdirectories there to your new node_modules
to see what's missing.

- 5,490
- 1
- 25
- 35
-
1The problem is i believe the person that originally created the project has been copying modules from somewhere else. So when i npm install after removing the current node modules the project stops working. – levif1 Jul 15 '19 at 15:19
-
Thanks @levif1. I've updated my answer to better deal with the situation you've described. – Jake Worth Jul 15 '19 at 15:23
-
-
As others pointed out, the issue is already there and one way of dealing with it is to rename the node_modules
folder, run npm install
or yarn install
and then try executing the application and figuring out the missing dependencies.
You can have an easier picture of what is missing if you run a tree
command in the old node_modules
and compare to a tree
command output from the new one. You wouldn't need to add all differences, just the top level ones.
Instead of tree
you can ls -1
(or dir /d /b
if on Windows).

- 16,030
- 6
- 37
- 69
In npm it's package.json that generates node_modules, not the other way around. You should get the package.json with all the dependencies and run npm install

- 86
- 5
-
The problem is i believe the person that originally created the project has been copying modules from somewhere else. So when i npm install after removing the current node modules the project stops working. – levif1 Jul 15 '19 at 15:19