clearing out node modules

2017-08-08
programming/ javascript/ nodejs/ bash

If you are like me and have lots of projects on the go, you will slowly start to see your disc drive fill up with all those node modules downloaded as project dependencies. This can cause problems if you like to backup your projects offline, lots and lots of small files means slower writes to disc with file transfers.

To solve the problem, I have created a small shell script (can be run on unix based terminals) to go through and remove the node_modules folder. If you keep your development work in the same parent directory, then simply create this script in the same parent folder and it will go and delete your node_modules. Run the following in terminal:

1
2
3
echo "#!/bin/bash

find . -name "node_modules" -type d -prune -exec rm -rf '{}' +" > remove_node_modules.sh

To run the script you will need to sure it has the correct permissions, use something like ‘chmod 710 remove_node_modules.sh’ to do this. To execute the script you can simply use the following:

1
./remove_node_modules.sh

When you want to rebuild and your project, you will need to first run the ‘npm install’ command in your project folder. This will re-download your build dependencies and hopefully your project will run as it did before the clear up when you run your app’s start command.