How to clean node_modules folder of packages that are not in package.json?

0 votes
Assume I install project packages with npm install that looks into package.json for modules to be installed. After a while I see that I don't need some specific module and remove its dependency from package.json. Then I remove some other modules from package.json because they are not needed anymore and others are replaced with alternatives.

Now I want to clean node_modules folder so that only modules listed in package.json stay there and the rest must go, something like npm clean. I know I can remove them manually but would like to have some nice ready to use sugar functionality for that.
Jul 13 in Node-js by kartik
• 20,980 points
101 views

1 answer to this question.

0 votes

Hello @kartik,

You could remove your node_modules/ folder and then reinstall the dependencies from package.json.

rm -rf node_modules/
npm install

This would erase all installed packages in the current folder and only install the dependencies from package.json. If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time.

Hope it helps!!

Thank You!!

answered Jul 13 by Niroj
• 44,800 points

Related Questions In Node-js

0 votes
1 answer

How to install a previous exact version of a NPM package?

Hello @kartik, If you have to install an ...READ MORE

answered Jul 8 in Node-js by Niroj
• 44,800 points
52 views
0 votes
1 answer

How to set environment variables from within package.json?

Hello @kartik, Set the environment variable in the ...READ MORE

answered Jul 13 in Node-js by Niroj
• 44,800 points
138 views
0 votes
1 answer

How to include scripts located inside the node_modules folder?

Hello @kartik, I would use the path npm ...READ MORE

answered Jul 15 in Node-js by Niroj
• 44,800 points
32 views
0 votes
1 answer

How to show the latest version of a package?

Hello @kartik, You can use: npm show {pkg} version (so npm ...READ MORE

answered Jul 15 in Node-js by Niroj
• 44,800 points
13 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 44,800 points
85 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5 in Java-Script by Niroj
• 44,800 points
147 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8 in PHP by Niroj
• 44,800 points
45 views
+1 vote
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8 in Node-js by Niroj
• 44,800 points
32 views
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13 in Node-js by Niroj
• 44,800 points
16 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

Hello @kartik, I have created the following, and ...READ MORE

answered Jul 16 in Node-js by Niroj
• 44,800 points
96 views