Auditing NPM Modules in Microservices
Clean-up some of the mess across your repositories
4 min read
Where I work at , we follow and have a good 200+ git repositories. With time, these reports become bloated with old NPM modules that are either outdated or not used anymore at all. I wanted to find a way to quickly audit and clean this mess.
I came across the great that:
- Tells you what's out of date.
- Provides a link to the package's documentation to decide if you want the update.
- Kindly inform you if a dependency is not used in your code.
- Works on your globally installed packages via
-g
. - Interactive Update for less typing and fewer typos via
-u
. - Supports public and private .
- Supports ES6-style syntax.
- Upgrades your modules using your installed version of npm, including the new
npm@3
, so dependencies go where you expect them. - Works with any public npm registry, , and alternate registries like .
- Does not query registries for packages with
private: true
in their package.json. - Emoji in a command-line app, because command-line apps can be fun too.
- Works with
npm@2
andnpm@3
, as well as newer alternative installers likeied
andpnpm
.
To automate running npm-check
across all of our reports and generate one coherent report, I created a simple plugin. The plugin, at its core, does the following:
This script is executed at the root folder that contains all of your repositories, will execute the npm-check
command, and aggregate the results in the npm-report.txt
at the root directory where you executed your script. The script also checks first if the npm-check
command exists or if we need to install it via an npm install -g npm-check.
To go step by step inside the main function:
find . -maxdepth 1 -type d \( ! -name . \)
: Will find all the directories within one level down of the current folderprintf 'Examining NPM modules for '{}''
will just print out a message indicating which folder we are currently examiningecho '{}' >> ../npm-report.txt
will print out the folder name examined in the output filenpm-report.txt
npm-check >> ../npm-report.txt
This will execute thenpm-check
command and pipe out the result into the output file
Auditing NPM Modules
After knowing the various modules used, I cleaned my file, pasted the results in an Excel sheet, sorted the cells, and created a subTotal on the count. This generated a list of all my NPM modules and their respective count.
Cleaning out unused NPM modules
The previous function gives us an idea of the modules and frequency used. However, we might have a bunch of unused modules that were left over old code and will just increase the size of our containers with no actual use.
We can easily clean out those modules by taking advantage of the and plug that in a similar wrapper as the function above:
I hope this helps you in cleaning out your repositories as well.