2

Why does showing npm version command takes more than 1 second? what does it do under the hood?

The command I run on my MacOS:

$ npm -v
8.15.0

Version: Monterey 12.6.1 (21G217)


Updated Feb 15

npm is a symlink to npm-cli.js and npm-cli.js only contains 2 lines of code. So the question is:

Why the 2 lines of code is so slow?

$ cat /usr/local/lib/node_modules/npm/bin/npm-cli.js 
#!/usr/bin/env node
require('../lib/cli.js')(process)



$ which npm
/usr/local/bin/npm

$ll /usr/local/bin/npm
lrwxr-xr-x  1 root  wheel  38 Sep 28 12:03 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
Richard
  • 148
  • 6
  • It's not a big problem for me. but I have a set of scripts which run show npm version when I open a terminal, it's quite annoying to see the script is hung at this command every time I open a terminal. – Richard Feb 15 '23 at 05:49
  • How did you install `npm`? – jsejcksn Feb 15 '23 at 05:58
  • I cannot recall how I installed it. Why does it matter? I updated the question. `npm` is a symlink to `npm-cli.js` and `npm-cli.js` only contains 2 lines of code. So the problem should be in the logic in the JS file but I'm new to js so don't understand why. :) – Richard Feb 15 '23 at 07:16

0 Answers0