0

My question

There are some criticisms against npm n, but they are dated (mostly from < 2018). Do the criticisms still stand? Is n unstable or does it have pitfalls? It bears saying that there were 115,014 downloads for n this week.

The problem

I want to install the latest stable versions of npm and nodeJS on Ubuntu 20.04, but that's surprisingly difficult.


Related:

5
  • May I flip the question? What do they say against NVM? Is there less confusion around NVM? Does it do the job reliably?
    – Levente
    Jan 9, 2021 at 5:05
  • @Levente these are good questions too, but the reason I'm also choosing n are: 1) it's a module of npm, not a bash script etc.; 2) it's actually very easy to use; 3) from what I saw in regard to nvm, setup seems a bit less convoluted. But I'm facing a problem: active node version points to /usr/bin/node, but I want it to point to my custom folder, where node v14 is installed ~/.n/bin/node. Lost some hours but I still don't know what to do here. I tried using n and selecting v14 (pressing Enter "to install"), but the active version isn't updated (i.e., v14 isn't installed?)
    – flen
    Jan 9, 2021 at 5:16
  • Solved my problem. I still had node installed (npm must have installed it). Because my changes to PATH were appended to the end of it, running node would always get me the old version 10 of node. I've updated my question to include all the steps to install it via npm n
    – flen
    Jan 10, 2021 at 5:34
  • It seems you are providing an answer to your question in your post? Then please move that info to a proper Answer, with the button on the bottom: "Answer your question" (which is a welcome thing to do on AskUbuntu).
    – Levente
    Apr 17, 2022 at 13:58
  • @Levente I think by now the question might have answered itself, I've never run into any problems using n. But the original question was whether there could be disadvantages. I'll move the steps into an answer, saying I never had issues, to make the question cleaner
    – flen
    Apr 18, 2022 at 4:43

2 Answers 2

1

After some years running this just fine, I think I can confidently say there are no real issues (at least, I didn't run into them). The only issue is getting n to work. For that, read below.

Solving it with npm n

I'm installing it as a user with sudo access and I don't ever want to sudo npm or node to make it work (why?).

  1. Install npm: sudo apt install npm.

  2. Set prefix for npm : npm config set prefix ~/.npm

  3. Upgrade npm to the latest version, npm i -g npm.

(Update: apt's npm is now so old that you should get an error message when trying to update, because the bundled node version is too old. In this case, run this command again at the end of all these instructions)

  1. Set environmental variables in ~/.profile file (these changes will only work after you log out and log in again, or if you run source ~/.profile):
# vars to avoid ever using sudo for npm
export PATH="$PATH:$HOME/.npm/bin"
# these are for n and where it'll install nodejs:
export N_PREFIX=$HOME/.n
export PATH="$PATH:$N_PREFIX/bin"

You might also want to add these lines to ~/.bash_profile, ~/.bash_login or ~/.bashrc in case you need to source ~/.profile every time you open a new terminal for npm, node and n to be recognized. See this for the reason.

  1. Run source ~/.profile to update the environmental variables. This way bash will find the path to npm and n during this session (i.e., without having to log out and then log in again).

  2. Use npm n package to upgrade node (see: https://askubuntu.com/a/663052/808646):

# Intructions: https://www.npmjs.com/package/n
# Some people also suggest npm cache clean -f, but (I think) this isn't necessary here. 
# I'm using lts for the stable release, for the latest use instead: n latest 

npm install -g n
n lts
  1. Uninstall the previous nodejs that came installed by npm: sudo apt-get purge nodejs .

Explanation: this step is crucial. So far, this is what happened: you installed npm, but it actually installed old nodejs and old npm on top of it. Although you purportedly "installed" a new nodejs using n, the previous nodejs takes precedence and n can't really install the new nodejs it downloaded, you must first remove the old one.
However, if you remove it without installing a new npm, you will not have npm anymore, because uninstalling old nodejs uninstalls old npm too (I think... or can it work if you just export all vars again? Maybe just having n installed with a nodejs/npm version will be enough?). Therefore, you must install a new npm (you did that in step 3) and a new nodejs using n (you did that in step 6) before uninstalling old nodejs. The reason why you also need a new nodejs install is that without a new nodejs, new npm won't run, it requires a current nodejs install.

  1. Check if it worked. If it didn't, try logging out and logging in again or enter n and select the correct nodejs version (*by default, it'll use the npm that's bundled with that nodejs version, but there are options to prevent this): node -v
0
0

Running sudo apt install npm works fine, but sudo apt-get --purge remove nodejs will also remove npm, putting you back where you started. So I don't see any way around using a non-apt approach for installing the latest version of nodejs.

8
  • Hello. This answer does not seem to have anything to do with the question as asked.
    – David
    Apr 14, 2022 at 12:03
  • Hi! Thank you for your comment (I see you posted this as an answer instead of comment, I hope some mod can delete it). You're right, I think they changed something and now npm is completely intertwined with nodejs, so if you purge the latter you purge the former. I'll rewrite my instructions to fix this
    – flen
    Apr 17, 2022 at 4:56
  • @BobKline I just updated the steps, if you follow them now they should work. Mods, please delete this answer
    – flen
    Apr 17, 2022 at 5:25
  • I would have preferred to post a comment, which would have been a more logical thing to do, but the forum blocked me from doing so, saying that I didn't have enough "points" for commenting but I did have enough points to post an "answer." Apparently I am allowed to post comments on my own answers. I'm not sure how these restrictions make sense, but it's not my system. Perhaps it would have been more helpful if those who are offended by the fact that I posted an answer could explain how else I would have been able to convey the problem I saw in the instructions. Thanks for fixing them.
    – Bob Kline
    Apr 17, 2022 at 11:24
  • I suppose it's also possible that by posting my comment as an "answer" I was pushed over the threshold enabling me to post real comments. If so, I will presumably lose that ability when the answer is deleted. 😩
    – Bob Kline
    Apr 17, 2022 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .