avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Checking the Size of node_modules Directories in Your Workspace

Fri Sep 22 2023

node_modules

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) named node_modules in the current directory (.) and all its subdirectories. The -prune option stops find from searching any further if it finds a node_modules directory. This prevents it from finding node_modules directories nested within other node_modules directories.

  • xargs du -sh: This part of the command calculates the size of each directory found by the find command. xargs is used to pass the list of directories to the du command. The -s option tells du to only include the total size of each directory, not the sizes of the individual files within them. The -h option 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!