n <version> command does not activate specified version
Asked Answered
R

4

20

Currently I have two versions of node installed on a Jenkins server.

$ n list
node/10.9.0
node/12.14.1

I'm trying to switch to version 10.9.0 for a specific build which requires it.

I've tried first by using n <version> but the latest 12.14.1 still shows as active:

$ n 10.9.0
installed : v10.9.0 to /opt/jenkins/n/bin/node
   active : v12.14.1 at /bin/node

I've also tried n use <version> which just follows up with a prompt.

$ n use 10.9.0
>

I've read various articles on this but could not get any commands to effectively switch versions.

i.e. https://blog.logrocket.com/switching-between-node-versions-during-development/

Below is the n exectuable.

which n
/opt/jenkins/n/bin/n

Versions appear to get installed under the below directory:

/opt/jenkins/n/n/versions/node/
10.9.0  
12.14.1

Below are the contents of /opt/jenkins/n/bin/

chrome-debug -> ../lib/node_modules/lighthouse/lighthouse-core/scripts/manual-chrome-launcher.js
lighthouse -> ../lib/node_modules/lighthouse/lighthouse-cli/index.js
n
ng
node
nodejs
npm -> ../lib/node_modules/npm/bin/npm-cli.js
npx -> ../lib/node_modules/npm/bin/npx-cli.js
n-uninstall
n-update

How can I switch node versions with n? Is there anything in the above that is missing? Thanks in advance.

Rheims answered 8/4, 2020 at 20:30 Comment(8)
check path, seems added too many path variableMaros
This is current path: echo $PATH /opt/rh/rh-python36/root/usr/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/jenkins/n/bin:/opt/jenkins/.local/bin:/opt/jenkins/bin I see /opt/jenkins/n/bin which is where all the node executables are. Is something missing from the path maybe or do you mean the contents of the folder are too much?Rheims
i think, there are another instance of node js in folder /opt/jenkins/binMaros
clean /opt/jenkins/ folder and install againMaros
Question: are node and nodejs the same thing? I have all of the n files under /opt/jenkins/n/bin. Only one nodejs is there. Interestingly the path shows /opt/jenkins/bin which doesn't existRheims
if u install using brew, apt-get, they call it node jsMaros
ok, I suspect there is some issue in the overall setup. Thanks for the help, will post back once I've made some progress.Rheims
/bin/node points to /opt/jenkins/n/bin/nodejs so after running n <version> this updates /opt/jenkins/n/bin/node only. It needs to be copied to nodejs cp /opt/jenkins/n/bin/node /opt/jenkins/n/bin/nodejs and after that the correct node version is used.Rheims
R
12

After running which node the path shows /bin/node.

/bin/node is a symlink to /opt/jenkins/n/bin/nodejs.

$ ll /bin/node
lrwxrwxrwx 1 root root 25 Jan 28 08:26 /bin/node -> /opt/jenkins/n/bin/nodejs

When installing, with n <version> it updates /opt/jenkins/n/bin/node only.

$ n 10.9.0
   installed : v10.9.0 to /opt/jenkins/n/bin/node
      active : v12.14.1 at /bin/node

Once this is done, node needs to be copied to nodejs.

$ cp /opt/jenkins/n/bin/node /opt/jenkins/n/bin/nodejs
$ node -v
v10.9.0
Rheims answered 9/4, 2020 at 12:44 Comment(0)
M
37

If you use brew and see something like this:

$ n                                                                                                                                          
     copying : node/16.19.1
   installed : v16.19.1 to /usr/local/bin/node
      active : v19.7.0 at /opt/homebrew/bin/node

Just run brew unlink node to let n manage node versions again.

Matthus answered 18/3, 2023 at 19:11 Comment(0)
R
12

After running which node the path shows /bin/node.

/bin/node is a symlink to /opt/jenkins/n/bin/nodejs.

$ ll /bin/node
lrwxrwxrwx 1 root root 25 Jan 28 08:26 /bin/node -> /opt/jenkins/n/bin/nodejs

When installing, with n <version> it updates /opt/jenkins/n/bin/node only.

$ n 10.9.0
   installed : v10.9.0 to /opt/jenkins/n/bin/node
      active : v12.14.1 at /bin/node

Once this is done, node needs to be copied to nodejs.

$ cp /opt/jenkins/n/bin/node /opt/jenkins/n/bin/nodejs
$ node -v
v10.9.0
Rheims answered 9/4, 2020 at 12:44 Comment(0)
A
5

You have copies of node installed to multiple locations, but only one of the locations is managed by n (which is set using N_PREFIX). Your PATH includes the copy installed by n later, so is not run when you just type node.

See also n doctor which looks for a few configuration issues including this one.

One possible solution is to have the location you want to use earlier in your path, another is to delete the node copy you don't want. However, you can run the n managed copy of node directly too which might be a good match for a CI type situation and not require any configuration changes.

n run (or n use) run the version of node you specify as a one-off command. e.g.

$ n run 10.9.0 --version
v10.9.0
$ n run 10.9.0  -e 'console.log("hello")'
hello
$ n run 10.9.0  my-script.js
Accost answered 9/4, 2020 at 21:55 Comment(1)
Thanks. I was able to get it working by copying the n updated /opt/jenkins/n/bin/node to /opt/jenkins/n/bin/nodejs which the bin/node symlink was pointing to. I tried n use in my initial post, as well as n run but I don't think I appended --version at the end which may be why I was left with a prompt.Rheims
K
1

Not sure it's the best option, but I manually did an export PATH= with whatever was the output of echo $PATH minus the substring /Users/achraf/.nvm/versions/node/v14.21.3/bin: (that was at the very beginning) and now my active version is not shadowed anymore.

Kyne answered 29/6, 2023 at 15:56 Comment(2)
nvm is another node manager and works by modifying the path. You could disable or uninstall it if you are using n.Accost
Kudos @shadowspawn! Just tried nvm deactivate and it printed: /Users/achraf/.nvm/*/bin removed from ${PATH} 🙌Kyne

© 2022 - 2024 — McMap. All rights reserved.