jorgebucaran / nvm.fish

The Node.js version manager you'll adore, crafted just for Fish
https://git.io/nvm.fish
MIT License
2.08k stars 69 forks source link

Gracefully handle npm not being installed #173

Closed eugenetriguba closed 2 years ago

eugenetriguba commented 2 years ago

Previous behavior:

> nvm use system
realpath: missing operand
Try 'realpath --help' for more information.
internal/modules/cjs/loader.js:818
  throw err;
  ^

Error: Cannot find module '/package.json'
Require stack:
- /home/eugene/.config/fish/[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at [eval]:3:30
    at Script.runInThisContext (vm.js:120:18)
    at Object.runInThisContext (vm.js:309:38)
    at Object.<anonymous> ([eval]-wrapper:10:26)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at evalScript (internal/process/execution.js:94:25) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/eugene/.config/fish/[eval]' ]
}
Now using Node v12.22.5 (npm )

Now, we'll simply list that it is not installed:

> nvm use system
Now using Node v12.22.5 (npm - not installed) /usr/bin/node
jorgebucaran commented 2 years ago

Thank you, @eugenetriguba.

How can npm not be present?

ljharb commented 2 years ago

node should always come with npm, but someone could do something silly and npm uninstall -g npm, i suppose

eugenetriguba commented 2 years ago

Hmm not sure how my install got into that state then. It was on a fresh Ubuntu install and I hadn’t done anything to remove npm. I’ll see if I can try to get a set of steps to reproduce it 🙂

eugenetriguba commented 2 years ago

Here's a minimally reproducible example :slightly_smiling_face:

$ docker pull ubuntu:21.10
$ docker run -it ubuntu:21.10 /bin/bash
root@193819ab21c1:/# apt update && apt upgrade
...
root@193819ab21c1:/# apt install nodejs
...
root@ed8c00e465dc:/# node --version
v12.22.5
root@ed8c00e465dc:/# npm --version
bash: npm: command not found

npm is a separate install via apt install npm.

On the page for the package, it shows npm as a "suggest" install but not a dependency.

I had installed yarn and hadn't had a need to install npm yet and was finding that behavior from the nvm script odd.

ljharb commented 2 years ago

(this is one of the many reasons node should never be installed via apt; it’s supposed to include npm)

eugenetriguba commented 2 years ago

It is nevertheless a situation that a user of nvm can run into though. I would think it would be desirable for nvm to handle that case then, regardless of the opinions around not including it with the nodejs package, since to a user it is a confusing experience.

That could be done by telling the user it is not installed, explicitly throwing an error that it is not installed with a more friendly error message, or at least documenting that nvm expects the system install to have nodejs and npm available.

Not unique to apt though. It is also an optional install with Arch Linux. I'm not sure on how many Linux distributions that is the case.

$ docker pull archlinux
$ docker run -it archlinux /bin/bash
[root@69653621239e /]# pacman -Syu
...
[root@69653621239e /]# pacman -S nodejs
...
[root@69653621239e /]# node --version
v17.6.0
[root@69653621239e /]# npm --version
bash: npm: command not found

https://archlinux.org/packages/community/x86_64/nodejs/

jorgebucaran commented 2 years ago

apt's node package should include npm instead.