I’ve run through many tutorials and courses over the years that have me install an npm package or two so I often forget what I have installed. Recently I looked up how to use the create-react-app package and, in their documentation, it stated if you had an older version of the package already installed you should uninstall it and use the new npx approach.

This led me to ask the question, “Do I have that installed?”. Here are the steps I took to answer that question

Give Me Everything

Copy to Clipboard

The npm ls command allows you to get a list of your installed packages. Here is the description from the npm documentation

This command will print to stdout all the versions of packages that are installed, as well as their dependencies, in a tree-structure.

Just entering in npm ls with no arguments will print out all the packages installed in the current directory

The output for this not only contains all the packages you have explicitly installed, it also contains all the dependencies that were installed for each package, so this list can get pretty long. While you could stop here and search through the output like it were a CVS receipt, we can do better!

Give Me Exactly What I Want

Copy to Clipboard

This will cull the list to the exact package you are looking for. If the package is installed as a dependency, it will include the full tree up to the root package.

This is fine if you know the exact name of the package you are looking for, but it doesn’t do partial text search.

Give Me What I Think I Want

Copy to Clipboard

This will return all the lines in the ls result that contain your search term.

Both the grep and Select-String commands have extensive options that allow you to pinpoint your search criteria if a simple text search isn’t enough for you.

Give Me Only What I Have Created

Copy to Clipboard

This will print out all the root level packages, which are all the packages you have explicitly installed.

Give Me The World

Copy to Clipboard

By adding the -g parameter, this will print out all the packages you have installed globally. The -g parameter can be added to any of the commands we have laid out above.

Give Me A Rose By Any Other Name

Copy to Clipboard

npm gives you a couple of aliases you can use if l and s are too far apart on the keyboard for you.

  • list – Exact same output as ls (but is an actual word)
  • la – extended output (even further away on the keyboard)
  • ll – extended output (now you’re just being lazy!)

Sources

Picture Attribution