Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 565  / 3 Years ago, fri, october 8, 2021, 8:40:31

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:



More From » apt

 Answers
6

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


[#2107] Sunday, October 10, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emuralm

Total Points: 290
Total Questions: 111
Total Answers: 117

Location: China
Member since Thu, Sep 1, 2022
2 Years ago
;