tj / n

Node version management
MIT License
18.77k stars 738 forks source link

can not switch version when using N_CACHE_PREFIX & N_PREFIX #814

Closed AmineDjeghri closed 2 days ago

AmineDjeghri commented 1 week ago

Bug Report

Summary

Cannot switch Node.js version when using N_CACHE_PREFIX and N_PREFIX. Executing n use or n auto command enters Node.js shell instead of switching version.

Steps to Reproduce

i already have a node version installed in /usr/local (from this image : nikolaik/python-nodejs:python3.11-nodejs22)

Expected Behaviour

22.6

Actual Behaviour

22.7

Other Information

This issue occurs when using N_CACHE_PREFIX and N_PREFIX environment variables TOGUETHER.

shadowspawn commented 1 week ago

A useful thing to try is running n doctor. This tests for a number of possible setup problems.

shadowspawn commented 1 week ago

Note: n use is an alias for n run and does not change the active version of node. It runs the specified version of node from the cache:

The environment variables are working for me.

Setup:

export N_PREFIX="$HOME/.n"
export N_CACHE_PREFIX="$HOME/.n-cache"
mkdir $N_PREFIX
mkdir $N_CACHE_PREFIX

PATH="$N_PREFIX/bin:$PATH"

n install 18
n install lts

Testing:

$ which node
/Users/john/.n/bin/node
$ node --version
v20.17.0
$ n which 18
/Users/john/.n-cache/n/versions/node/18.20.4/bin/node
$ n run 18 --version
v18.20.4
AmineDjeghri commented 1 week ago

@shadowspawn

I have a makefile :

install_nvm_n:
    @echo "Checking if NVM is installed in $(NVM_DIR)"
    @if [ -s "$(NVM_DIR)/nvm.sh" ]; then \
        . "$(NVM_DIR)/nvm.sh" && \
        nvm -v; \
    else \
        echo "NVM not found"; \
        echo "Installing NVM"; \
        wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash; \
    fi

    # installing npm
    echo "Installing npm"
    . "$(NVM_DIR)/nvm.sh" && nvm install node && npm -v

    @echo "Installing node auto & node modules locally..."
    @export N_CACHE_PREFIX="node" N_PREFIX="node" ; \
    . "$(NVM_DIR)/nvm.sh"; \
    npm ci; \
    ./node_modules/.bin/n doctor; \
    ./node_modules/.bin/n install 22.6; \
    ./node_modules/.bin/n doctor

result is


Checking if NVM is installed in /home/amine/.nvm
0.40.1
# installing npm
echo "Installing npm"
Installing npm
. "/home/amine/.nvm/nvm.sh" && nvm install node && npm -v
v22.8.0 is already installed.
Now using node v22.8.0 (npm v10.8.2)
10.8.2
Installing node auto & node modules locally...
npm warn EBADENGINE Unsupported engine {
npm warn EBADENGINE   package: undefined,
npm warn EBADENGINE   required: { npm: '>=10.0.0 <11.0.0', node: '=22.6.0' },
npm warn EBADENGINE   current: { node: 'v22.8.0', npm: '10.8.2' }
npm warn EBADENGINE }

added 528 packages, and audited 529 packages in 5s

58 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
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
/usr/bin/bash
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

  installing : node-v22.6.0
       mkdir : node/n/versions/node/22.6.0
       fetch : https://nodejs.org/dist/v22.6.0/node-v22.6.0-linux-x64.tar.xz
     copying : node/22.6.0                                                                                                                                                                                                          
find: ‘node/n/versions/node/22.6.0/lib’: No such file or directory
cp: cannot stat 'node/n/versions/node/22.6.0/bin/node': No such file or directory
find: ‘node/n/versions/node/22.6.0/include’: No such file or directory
find: ‘node/n/versions/node/22.6.0/share’: No such file or directory
find: ‘node/n/versions/node/22.6.0/share/man’: No such file or directory
./node_modules/.bin/n: line 746: node/bin/node: No such file or directory
   installed :
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
/usr/bin/bash
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

n
/run/user/1000/fnm_multishells/129135_1725489130896/bin/n
v9.2.3

node
/home/amine/.nvm/versions/node/v22.8.0/bin/node
v22.8.0
JavaScript engine: v8

npm
/home/amine/.nvm/versions/node/v22.8.0/bin/npm
10.8.2

tar
/usr/bin/tar
tar (GNU tar) 1.34
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by John Gilmore and Jay Fenlason.

curl or wget
/usr/bin/curl
curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.18
Release-Date: 2022-01-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd

uname
Linux AMINE-R7-3070TI 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

SETTINGS

n
node mirror: https://nodejs.org/dist
node downloads mirror: https://nodejs.org/download
install destination: node

# path en variables here (i removed them)

Proxy

CHECKS

Checking n install destination is in PATH...
'node/bin' is not in PATH

Checking n install destination priority in PATH...
good

Checking npm install destination...
good

Checking prefix folders...
good

Checking permissions for cache folder...
good

Checking permissions for install folders...
good

Checking mirror is reachable...
good
shadowspawn commented 1 week ago

nvm is a node version manager. n is a node version manager.

They are two different utilities and take two different approaches, and you can't use both at once.

shadowspawn commented 1 week ago
@export N_CACHE_PREFIX="node" N_PREFIX="node"

You need an absolute location for the prefix environment variables. Like say:

@export N_CACHE_PREFIX="$HOME/node" N_PREFIX="$HOME/node"

But you only need to define N_CACHE_PREFIX if you don't like where it gets put by default under N_PREFIX.

AmineDjeghri commented 1 week ago

@shadowspawn

Thank you for your quick answers, You are totally right, i switched to absolute paths and everything is working now !

Do you know how can i install n without installing nvm ? as you can see in my script, i install nvm then node just to get npm & n...

AmineDjeghri commented 1 week ago

last question

doing this in the make file script doesn't switch to the appropriate node version but enters the node console :

    ./node_modules/.bin/n install auto; \
    ./node_modules/.bin/n use 22.6

result :

found 0 vulnerabilities
       found : /home/amine/git/ai-cloud-project-template/package.json
        read : =22.6.0
      target : 22.6.0
     copying : node/22.6.0
   installed : v22.6.0 to /home/amine/git/ai-cloud-project-template/node_local/bin/node
      active : v22.8.0 at /home/amine/.nvm/versions/node/v22.8.0/bin/node
Welcome to Node.js v22.6.0.
Type ".help" for more information.
>
shadowspawn commented 1 week ago

The README has an example of using n to install the lts version of node without either n or node being already installed:

curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s lts
# If you want n installed, you can use npm now.
npm install -g n

That curl command is actually downloading n, so you could also just save it yourself. See example: https://github.com/tj/n/issues/809#issuecomment-2264129789

shadowspawn commented 1 week ago

When you run n install auto it installs the target version (which in your example is 22.6.0).

You don't then have to activate it using use. That is an nvm pattern.

As I noted earlier, n use is an alias for n run and does not change the active version of node. It runs the specified version of node from the cache. You are telling n to run node v22.6.0, which it does, and drops you into the node console.