1

Basically I am looking for the "opposite" of npm prune or this SO question.

More specifically:

I am looking to clean up node_modules folder from all packages that are listed in my root package.json file. Sort of a fresh start before npm install.

The reason I don not want to simply rm -rf node_modules/ is because I have some local modules that I don't want to get deleted.

Community
  • 1
  • 1
Michael
  • 22,196
  • 33
  • 132
  • 187

3 Answers3

1

it isnt possible to remove at once all in your package.json you could write shell script to loop through. or you can check npm ls and then remove npm rm <name> or npm uninstall each manually. and to update it in package.json simultaneously npm rm <name> --save

A.B
  • 20,110
  • 3
  • 37
  • 71
1

A better approach would be to have your permanent (local) modules in a directory of a higher level:

-node_modules (local)
-my_project
|-node_modules (npm)

That way, when you wipe the node_modules directory, the outer local modules remain.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

As pointed out by others, there's no native npm command to do that.

I've taken this answer and modified the command:

$ npm ls | grep -v 'npm@' | awk '/@/ {print $2}' | awk -F@ '{print $1}' | xargs npm rm

This lists all of your local packages and then npm rm them one by one.

For convenience, if you want, you can add the following line to your package.json:

"scripts": {
    "uninstall": "npm ls | grep -v 'npm@' | awk '/@/ {print $2}' | awk -F@ '{print $1}' | xargs npm rm"
}

and then run

$ npm run uninstall
Community
  • 1
  • 1
Michael
  • 22,196
  • 33
  • 132
  • 187