Two little-known useful npm commands

Orbay Yeşil
4 min readDec 9, 2021

--

I am going to tell you about two npm commands. I think they are not very popular, however they may be helpful for solving some of your development environment problems when you get stuck.

These commands are:

  • npm list (npm ls)
  • npm config list (npm config ls)

1. npm list/ls

There are tons of packages on npm and lots of them usually use others as a dependency. Also, these packages with this dependency use other packages as a dependency, too.

This depth of dependency might have no end, and you sometimes need to find a specific package name which is being used in your project. “npm list” helps you for this reason. Let’s try it with one of my projects:

You can see the dependency tree in your project like this. For my project, it generated more than 900 lines as tree view, and I had to cut the screenshot to fit it here.

Sometimes, it may be difficult to find out a specific project name on the terminal window. You can export it to a file like that: “npm list > dependencyList.txt”

Some useful configurations for “npm list”

  • --depth

In some cases, you need to look up globally-installed/first level packages. This way you can find them out by using “--depth=0” configuration:

Also, if you need another level of depth, you can configure it like this: “ --depth=1, 2, 3, …”

  • --json

“npm list” tree view output has a distinctive syntax, and it may sometimes be confusing. Actually, following the depth lines is really hard for me. If you feel like me, you can use “--json” configuration to create a json output:

2. npm config list/ls

If you work in a large company, or your company has a private npm packages registry, probably you sometimes use the public registry(registry.npmjs.org) and sometimes your company’s private registry.

npm uses “.npmrc” file to manage the registration. For instance, if you want to switch the registry to your company’s private registry server, you should write this registry server address in your “.npmrc” file like this:

registry=http://my-company-registry-server/

It means npm will use this server as a registry address every time till you change it.

So far everything is ok, but somethings are now starting to get messy. “.npmrc” file is not singular. Although, usually it is located in the user folder(for windows) in your computer’s disk, it may also be in more than one location(for example; in your project folder). Further more, the location where you want to run the npm command(for instance; npm install) is crucial to decide which registry will be used by npm on this command.

For example; you have two .npmrc files, one of them are located in the user folder as “registry=http://first-registry-server/”, the other is located in the your project folder as “registry=http://second-registry-server/”, and you want to run “npm install” command. If you run it under your project location; npm will install the packages from the “second-registry-server”. In contrast, if you run it outside of the project location, npm will install the packages from the “first-registry-server”.

You might think it is manageable, but sometimes it may be confusing and hard to understand why npm does not find your packages, because you may think about other options to find the problem and you might miss the main point. For this reason, I recommend you use the “npm config list” command at these times. It will give you a dump about your npm configurations, and there you will be able to see the information about your registry as “metrics-registry” key:

Thanks so much for reading!

--

--