Reputation: 13433
Is there an easy way to obtain a count of installed npm packages, both global and local to an application?
For example, after running create-react-app
[1], I get screens-full of packages and dependencies installed when I enter the npm ls
command.
How to find a count ("cardinality") of said packages and dependencies eludes me. Tyvm, Keith :^)
References:
Upvotes: 17
Views: 11866
Reputation: 827
This will output the number of distinct production (not dev) dependencies, traversing an entire graph (that is, it'll not count different versions of the same dependency):
echo $(($(npm ls -P --depth Infinity --parseable | sed 's/@[0-9].*$//g' | uniq | wc -l) - 1))
I. e. if a graph has both [email protected]
and [email protected]
, it will be counted only once.
To verify correctness, remove node_modules
dir, then run npm i
and check the beginning of the output saying something like added XXX packages, and ...
If you'd like to include different versions of the same dependency into the count, this is the slightly modified command:
echo $(($(npm ls -P --depth Infinity --parseable | uniq | wc -l) - 1))
npm 9.8.1
pnpm 8.11.0
node v18.18.2
zsh 5.9 (x86_64-apple-darwin22.0)
macOS 13.6.3
P. S.
Both of the commands work with pnpm
as well, however the output doesn't matches the output of pnpm install
... I don't see enough economic incentive to be solving that mis(t)ery. Please leave a comment if you see it otherwise.
Upvotes: 1
Reputation: 585
In order to count unique dependencies you should exclude deduped packages:
npm ls | sed '/deduped$/d' | wc -l
Upvotes: 9
Reputation: 9030
In the latest version of npm
, it should list a count of how many packages were installed/added after running npm install
(or npm i
):
In this case the project I'm working has only a few top-level dependencies
and devDependencies
listed in the package.json
but 281
is actual count of all packages added. I posted a separate question in the hopes of getting more information on the audit count discrepancy.
So I think what I listed above is the simplest approach for getting the count for a specific project. However let's break your question down a little more...
See above (basically remove your node_modules
directory and run npm i
).
The OP's question touches on this with create-react-app
. So what if I want to see the total package count for create-react-app
?
npm i create-react-app
.Why create a new directory you ask? Because an existing project might already have some of create-react-app
s dependencies installed, thus causing the added
count to differ:
I don't have a great answer to this one yet (but I'm happy to update the answer if we come across one). Because global installs (npm i -g ...
) aren't store in a global package.json
, I don't think you can run npm i -g
by itself to get the count. It seems one approach might be to do what the OP mentioned, meaning...
npm ls
.And I'm sure some bash wizard could automate this by piping the output of npm ls
to another CLI command or two, e.g. (npm ls | count-lines
).
One thing to note here though is that I'm not 100% sure the number of lines is a one-to-one mapping for the number of packages. I seem to recall instances where npm
will list
a dependency under a package but then put parentheses next to it indicating that it was installed only once (but listed multiple times), is symlinked, etc. So again, not sure of a great solution for globals yet.
UPDATE:
npm ls
will list certain dependencies with deduped
next to them like so:
[email protected] deduped
So npm ls
with a straight line count isn't a perfect approach unless you delete deduped
lines and take into account other flags like this one.
Upvotes: 5