tj / n

Node version management

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extra logging for when installed npm is not active after install, or extra test in doctor

googol7 opened this issue · comments

Short Version

When I ran n it didn’t install the bundled npm version. I had to uninstall the globally installed npm. Now it works.

Long Version

When I switched the Node version with n I just saw:

     copying : node/14.21.2
   installed : v14.21.2

I expected to see:

     copying : node/14.21.2
   installed : v14.21.2 (with npm 6.14.17)

I didn’t preserve the npm version.

n doctor didn’t tell me why.

Solution

I uninstalled the globally installed npm to let n also switch to the bundled npm version:
npm uninstall -g npm

Feature request

n should be a little bit more verbose and tell us why it doesn’t also switch the bundled npm version. The message could be:

     copying : node/14.21.2
   installed : v14.21.2
        hint : Didn’t switch the bundled `npm` version because it is installed globally.

The hint could also be:
hint : Didn’t switch the bundled npm version because the environment variable N_PRESERVE_NPM is set.

References

Configuration Details

$ n --version
v9.0.1

$ command -v node
/usr/local/bin/node

$ node -p process.platform
darwin

$ n doctor
This information is to help you diagnose issues, and useful when reporting an issue.
Note: some output may contain passwords. Redact before sharing.


COMMAND LOCATIONS AND VERSIONS

bash
/bin/bash
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin22)
Copyright (C) 2007 Free Software Foundation, Inc.

n
/Users/phil/.npm/bin/n
v9.0.1

node
/usr/local/bin/node
v9.11.2
JavaScript engine: v8

npm
/usr/local/bin/npm
5.6.0

┌─────────────────────────────────────────────────────────┐
│                 npm update check failed                 │
│           Try running with sudo or get access           │
│          to the local update config store via           │
│ sudo chown -R $USER:$(id -gn $USER) /Users/phil/.config │
└─────────────────────────────────────────────────────────┘

tar
/usr/bin/tar
bsdtar 3.5.3 - libarchive 3.5.3 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.8

curl or wget
/usr/bin/curl
curl 7.86.0 (x86_64-apple-darwin22.0) libcurl/7.86.0 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.11 nghttp2/1.47.0
Release-Date: 2022-10-26
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL threadsafe UnixSockets

uname
Darwin Traktor.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000 arm64


SETTINGS

n
node mirror: https://nodejs.org/dist
node downloads mirror: https://nodejs.org/download
install destination: /usr/local
PATH: /Users/phil/.npm/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
ls-remote max matches: 20

Proxy


CHECKS

Checking n install destination is in PATH...
good

Checking n install destination priority in PATH...
good

Checking permissions for cache folder...
good

Checking permissions for install folders...
good

Checking mirror is reachable...
good

I suspect you had npm installed in a custom location, separate from where node was installed. I see /Users/phil/.npm/bin in your PATH. There is extra code in n to do logging showing when the active node is not the one that n installed, but not for npm.

What does npm prefix -g show?

(A custom prefix might be set in your .npmrc file or using an environment variable.)

➜ npm prefix -g
/Users/phil/.npm

┌─────────────────────────────────────────────────────────┐
│                 npm update check failed                 │
│           Try running with sudo or get access           │
│          to the local update config store via           │
│ sudo chown -R $USER:$(id -gn $USER) /Users/phil/.config │
└─────────────────────────────────────────────────────────┘

I reproduced the issue by setting the npm prefix, installing an npm there, and modifying my PATH to have that first. The underlying problem is that the freshly installed version of npm is not the first found in the PATH. The existing logging omits the npm information in this case, so at least accurate as far as it goes! 😄

The extra logging for when this happens with node is at:

n/bin/n

Lines 734 to 735 in 6bb001e

log "installed" "$("${installed_node}" --version) to ${installed_node}"
log "active" "$("${active_node}" --version) at ${active_node}"

The test in n doctor for multiple versions of node in PATH starts at:

n/bin/n

Line 1465 in 6bb001e

if command -v node &> /dev/null; then

The multiple versions in PATH could also happen with other installed binaries like npx and corepack, although npm is the only one that gets special treatment in the messages currently.

(A small subset of users may deliberately install npm global modules to a custom directory so they can manage it independently of node.)

Thanks for the detailed description, including looking to n doctor for clues.

What do you think about the suggested additional hints?

If you are asking about the specific hints, neither of the suggested hints are quite what I would want.

Didn’t switch the bundled npm version because it is installed globally.

The problem isn't that it is installed globally as such, the problem is that it is installed somewhere different to the location n used. The same problem could occur if the system package manager writes a version to somewhere different (on Linux) or a third-party package manager like HomeBrew has a copy installed elsewhere.

There is an additional factor in this particular issue that a custom global prefix has been set. That is going to affect future npm installs. But it is the expected behaviour the user has opted into (a bit like the following).

Didn’t switch the bundled npm version because the environment variable N_PRESERVE_NPM is set.

The user has set the environment variable, so I don't think n needs to tell them every time they change versions of node.


If you are asking what I thought about having additional information about npm issues with the install, I am still thinking about that. It has not come up a lot (or at least not compared with multiple node versions!) But having some clues so new users can hopefully diagnose setup issues is good, if it does not introduce too much noise for "normal" scenarios.

A fallback is extending the n doctor checks. Ok for noisy output since the user is looking for information.

I didn’t mean the specific hint I suggested. These were just examples. I wasn’t even aware that n can also change the npm version as I've never seen that on my machine. Only recently found out that it can do it when I saw it on the screen of a friend. I think it would be good to see at least a hint like

Didn’t change the npm version. Run n doctor.

What do you think?

Possible, yes.

I did actually have something similar in an early version of the extra messages for a shadowed node install with (try running 'nvh doctor'): shadowspawn/nvh#3 (comment)

I went with just the installed/active lines, but people don't always work out what it means.

Opened a PR adding a check to n doctor: #764

Added npm check to n doctor in v9.1.0. Won't add to install logging for now, see if comes up again.

Thanks @googol7