The Node Version Manager(nvm) could help you to manage multiple Node.js installations. The following steps are executed from root account.
1. Install the required packages.
apt-get install build-essential openssl libssl-dev curl
2. Create a new user group and add those users which are allowed to manage Node.js installation. The following command create a new group called dev.
groupadd dev
3. Download the nvm source from GitHub to /opt/nvm.
git clone https://github.com/creationix/nvm.git /opt/nvm
4. Create the /usr/local/nvm directory which serves as the NVM_DIR and stores all the downloaded Node.js.
mkdir /usr/local/nvm
5. Create the /usr/local/node directory which serves as the NPM_CONFIG_PREFIX and stores all the Node Package Manager(npm) global files.
mkdir /usr/local/node
6. Make sure the dev group could access the 2 newly created folders.
chown -R root:dev /usr/local/nvm chmod -R 775 /usr/local/nvm chown -R root:dev /usr/local/node chmod -R 775 /usr/local/node
7. Create the /etc/profile.d/nvm.sh as follow. This file would be executed every time when any user is logged in.
export NVM_DIR=/usr/local/nvm source /opt/nvm/nvm.sh export NPM_CONFIG_PREFIX=/usr/local/node export PATH="/usr/local/node/bin:$PATH"
8. Open another SSH session and login as a user under the dev group. Verify the nvm installation as follow.
nvm --version
9. Install a specific version of Node.js.
nvm install 0.11
10. Verify the Node.js version.
node --version
11. Set your default Node.js version so it would be activate automatically when user is logged in.
nvm alias default 0.11
12. Or you can switch to any installed version by npm use.
nvm use 0.11
Done =)
Reference:
Nice! I just need this for my RPi 😉
LikeLike
ya~ this is very useful just like RVM for ruby~ =D
LikeLike
Hey, you didn’t mention how to install! 😛
LikeLike
Sorry, my fault! Zsh can’t load the profile script 😛
LikeLike
haha~ so where did u put the profile script?
LikeLike
Zsh has global profile as well in /etc/zsh
zprofile or zshrc. I add “. /etc/profile” in zprofile to fix it.
However, I haven’t created group, it has expected problem to run nvm install.
I want to know the senario. Is it always need to go back old version for using node.js? I’m thinking if it’s sysadmin may be I would install different versions but not let user to install themselves
LikeLike
i think it’s not easy to rollback to old version of node.js. probably can just move forward. this is because some node modules may required a newer node.js version, so if u rollback the node.js, some node modules may fail.
in my case, becoz is a dev server, so i let all developers decide which node.js version they want to use. but actually the node.js version is user dependent. Assume by default everyone is using 0.11, if User A switch to 0.10, it won’t affect other users’ node.js version.
in case u are running the application with a specific user, it should not be affected by other users.
LikeLike
May be I prefer/get used to a more restrictive env. I’m against to groupadd or let developer to touch /usr/local 🙂
Let me try if it works only root can run nvm install. See if users can still nvm use
Seems, nvm provided bash_completion script. May be we don’t need to add our profile script
LikeLike
yes. maybe can run the install script by root account so only root can execute the nvm.
for the developers, just set the following env varaibles.
not sure if it will work~ XD
LikeLike
I get the following error message after install node (v12.0.7) – `nvm is not compatible with the “NPM_CONFIG_PREFIX” environment variable: currently set to “/usr/local/node”`. Any idea whats wrong?
I am using nvm version 0.29.0 btw.
LikeLiked by 1 person
Maybe related to this issue.
oh-my-zsh: NVM is not compatible with the npm config “prefix” option
LikeLike
I am receiving the same error:
Downloading https://nodejs.org/dist/v5.3.0/node-v5.3.0-linux-x64.tar.xz…
######################################################################## 100.0%
WARNING: checksums are currently disabled for node.js v4.0 and later
nvm is not compatible with the “NPM_CONFIG_PREFIX” environment variable: currently set to “/usr/local/node”
Run `unset NPM_CONFIG_PREFIX` to unset it.
I am using nvm v0.30.1. While if worked after i unset NPM_CONFIG_PREFIX but i am not sure where the modules were installed.
LikeLike
see if you could find a solution at GitHub issue link from this comment.
LikeLike
I looked through that link in details and several other articles on the web. What I found out was setting NPM_CONFIG_PREFIX is anti pattern and they explicitly disabled it in the new version of NODEJS. I fixed it by running a command to copy the contents of the nvm default version to /usr/lib/node everytime it installs a new version.
LikeLike
cool. hope they could fix the problem later without manual hack.
LikeLike
thanks!
LikeLike