Checking the Size of node_modules Directories in Your Workspace

Node.js developers often find themselves dealing with large node_modules directories. These directories can take up a lot of disk space, especially if you're working on multiple projects. In this post, we'll show you how to check the size of all node_modules directories in your workspace using the terminal.
The Tools
We'll be using two commands: find and du. The find command is used to locate files and directories, and the du command is used to estimate file and directory space usage.
The Command
Here's the command we'll be using:
find . -name "node_modules" -type d -prune | xargs du -sh
Let's break this down:
find . -name "node_modules" -type d -prune: This part of the command is looking for directories (-type d) namednode_modulesin the current directory (.) and all its subdirectories. The-pruneoption stopsfindfrom searching any further if it finds anode_modulesdirectory. This prevents it from findingnode_modulesdirectories nested within othernode_modulesdirectories.xargs du -sh: This part of the command calculates the size of each directory found by thefindcommand.xargsis used to pass the list of directories to theducommand. The-soption tellsduto only include the total size of each directory, not the sizes of the individual files within them. The-hoption makes the output human-readable, converting bytes into kilobytes, megabytes, gigabytes, etc., as necessary.
Calculating the Total Size
If you want to calculate the total size of all node_modules directories combined, you can add the -c option to the du command:
find . -name "node_modules" -type d -prune | xargs du -sch
The -c option tells du to produce a grand total at the end of its output.
Conclusion
Knowing how to check the size of your node_modules directories can be very useful, especially when you're trying to free up disk space. By using the find and du commands, you can easily find out how much space each node_modules directory is taking up, and even get a total of all the node_modules directories combined. Happy coding!