Why does node is not available to all users with NVM? - node.js

It's probably a newbie question but I'm wondering why when I install node with nvm, it is only available for this user (it's not "global").
Let's say I'm log into the server with a user "admin":
curl https://raw.githubusercontent.com/creationix/nvm/v0.7.0/install.sh | sh
source ~/.profile
nvm install 0.10.30
nvm use 0.10.30
node -v
# outputs v0.10.30
Node is up and running for this user but when I switch to the root:
su
node -v
It displays:
The program 'node' can be found in the following packages:
* node
* nodejs-legacy
Try: apt-get install <selected package>
Why that? Is there a way to install node and make it available to all users? (I don't want to reinstall every time I need it for a new user.)

The problem is that NVM installs node.js to a user's local directory, and updates that user's .profile.
Here's a one line script that can copy your install to /usr/local/bin, where everybody can use node.js:
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

Related

sudo: npm: command not found in WSL

I'm trying to install globally some packages for my Unix environment on Windows with WSL. I use nvm to manage the different versions of Node.js.
The problem is while using the sudo command before a global npm install :
sudo npm install --global prompt-pure
I get an error: sudo: npm: command not found !
Doing a simple npm install --global pure-prompt will work, but as I'm not super user, the global installation ends up with a permission error.
How can I fix this annoying problem and keep nvm ?
Thanks by advance
When you try to run sudo npm, it tries to run the npm binary file /usr/bin/npm but your binary is located in a different place, which can be found running which npm.
Example: /home/damo/.nvm/versions/node/v8.11.1/bin/npm
The solution is to create a link in /usr/bin/ pointing to the actual binary:
sudo ln -s "$(which npm)" /usr/bin/npm
You can also add the following link so you can run sudo node
sudo ln -s "$(which node)" /usr/bin/node
For me I needed to actually cheat and run as root before installing node as root. For this I ran sudo su in a Ubuntu WSL term and then installed node.
Once I did that I could sudo su then npm run special-script.
I don't know a better way to get a script to attach to restricted ports like 443 for testing https connections but it works.
As you will find in the man file for sudo man sudo sudo will execute a command as another user. That other user has a different home directory to you and access to different commands
When i run sudo which node i get nothing, but which node returns /home/damo/.nvm/versions/node/v8.11.1/bin/node
Lets look at your actual goal. You say you are trying to install pure-prompt, i know this does not ask your explicit question but given you have zsh installed have you tried oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh) i use this on every install of linux i ever have to work with (VMs, WSL, docker). Very customizable and looks great out of the box.

Gulp global installation not possible [duplicate]

NodeJS interpreter name(node) on Ubuntu has been renamed to nodejs because of a name conflict with another package. Here's what the readme. Debian says:
The upstream name for the Node.js interpreter command is "node".
In Debian the interpreter command has been changed to "nodejs".
This was done to prevent a namespace collision: other commands use
the same name in their upstream, such as ax25-node from the "node"
package.
Scripts calling Node.js as a shell command must be changed to instead
use the "nodejs" command.
However, using nodejs mucks up installing packages using npm. Package installation fails with the following error:
sh: 1: node: not found
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian
How do I make npm understand that nodejs is already installed on the system but the interpreter name is different?
TL;DR:
sudo apt-get install nodejs-legacy
First of all let me clarify the situation a bit. In summer 2012 Debian maintainers decided to rename Node.js executable to prevent some kind of namespace collision with another package. It was very hard decision for Debian Technical Committee, because it breaks backward compatibility.
The following is a quote from Committee resolution draft, published in Debian mailing list:
The nodejs package shall be changed to provide /usr/bin/nodejs, not /usr/bin/node. The package should declare a Breaks: relationship with
any packages in Debian that reference /usr/bin/node.
The nodejs source package shall also provide a nodejs-legacy binary package at Priority: extra that contains /usr/bin/node as a symlink to
/usr/bin/nodejs. No package in the archive may depend on or recommend
the nodejs-legacy package, which is provided solely for upstream
compatibility. This package declares shall also declare a Conflicts:
relationship with the node package.
<...>
Paragraph 2 is the actual solution for OP's issue. OP should try to install this package instead of doing symlink by hand. Here is a link to this package in Debian package index website.
It can be installed using sudo apt-get install nodejs-legacy.
I have not found any information about adopting the whole thing by NPM developers, but I think npm package will be fixed on some point and nodejs-legacy become really legacy.
Try linking node to nodejs. First find out where nodejs is
whereis nodejs
Then soft link node to nodejs
ln -s [the path of nodejs] /usr/bin/node
I am assuming /usr/bin is in your execution path. Then you can test by typing node or npm into your command line, and everything should work now.
You can also install Nodejs using NVM or Nodejs Version Manager There are a lot of benefits to using a version manager. One of them being you don't have to worry about this issue.
Instructions:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
Once the prerequisite packages are installed, you can pull down the nvm installation script from the project's GitHub page. The version number may be different, but in general, you can download and install it with the following syntax:
curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh
This will download the script and run it. It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.
To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:
source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can type:
nvm ls-remote
. . .
v0.11.10
v0.11.11
v0.11.12
v0.11.13
v0.11.14
As you can see, the newest version at the time of this writing is v0.11.14. You can install that by typing:
nvm install 0.11.14
Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:
nvm use 0.11.14
When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:
node -v
The comeplete tutorial can be found here
Install nvm first using:
curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
Run command
source ~/.profile
Now run this and this will show will all installed or other versions of packages:
nvm ls-remote
Installed packages will be in green. Install whatever version you want:
nvm install 6.0.0
Check where is not installed:
which node
Check current version:
node -v
n=$(which node);
n=${n%/bin/node};
chmod -R 755 $n/bin/*;
sudo cp -r $n/{bin,lib,share} /usr/local
sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs
sudo apt-get install nodejs-legacy
source ~/.profile
Combined the accepted answer with source ~/.profile from the comment that has been folded and some clean up commands before. Most likely you will also need to sudo apt-get install npm after.
for me problem was solved by,
sudo apt-get remove node
sudo apt-get remove nodejs
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
alias node=nodejs
rm -r /usr/local/lib/python2.7/dist-packages/localstack/node_modules
npm install -g npm#latest || sudo npm install -g npm#latest
Here's another approach I use since I like n for easy switching between node versions.
On a new Ubuntu system, first install the 'system' node:
curl -sL https://deb.nodesource.com/setup | sudo bash -
Then install n module globally:
npm install -g n
Since the system node was installed first (above), the alternatives system can be used to cleanly point to the node provided by n. First make sure the alternatives system has nothing for node:
update-alternatives --remove-all node
Then add the node provided by n:
update-alternatives --install /usr/bin/node node /usr/local/bin/node 1
Next add node provided by the system (the one that was installed with curl):
update-alternatives --install /usr/bin/node node /usr/bin/nodejs 2
Now select the node provided by n using the interactive menu (select /usr/local/bin/node from the menu presented by the following command):
update-alternatives --config node
Finally, since /usr/local/bin usually has a higher precedence in PATH than /usr/bin, the following alias must be created (enter in your .bashrc or .zshrc) if the alternatives system node is to be effective; otherwise the node installed with n in /usr/local/bin takes always precedence:
alias node='/usr/bin/node'
Now you can easily switch between node versions with n <desired node version number>.
On Linux Mint 17, I tried both solutions (creating a symlink or using the nodejs-legacy package) without success.
The only thing that finally worked for me was using the ppa from Chris Lea:
sudo apt-get purge node-*
sudo apt-get autoremove
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
This installed node version 10.37 and npm 1.4.28. After that, I could install packages globally.
As other folks already mention, I will suggest not to use "sudo apt-get" to install node or any development library. You can download required version from https://nodejs.org/dist/v6.9.2/ and setup you own environment.
I will recommend tools like nvm and n, to manage you node version. It is very convenient to switch and work with these modules.
https://github.com/creationix/nvm
https://github.com/tj/n
Or write basic bash to download zip/tar, extract move folder and create a soft link. Whenever you need to update, just point the old soft link to new downloaded version.
Like I have created for my own, you can refer:
https://github.com/deepakshrma/NodeJs-4.0-Reference-Guide/blob/master/nodejs-installer.sh
#Go to home
cd ~
#run command
#New Script
wget https://raw.githubusercontent.com/deepakshrma/NodeJs-4.0-Reference-Guide/master/nodejs-installer.sh
bash nodejs-installer.sh -v lts
#here -v or --version can be sepecific to 0.10.37 or it could be latest/lts
#Examples
bash nodejs-installer.sh -v lts
bash nodejs-installer.sh -v latest
bash nodejs-installer.sh -v 4.4.2
Simple solution from here
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash --
sudo apt-get install nodejs
You can specify version by changing setup_x.x value, for example to setup_5.x
Your System is not able to detect the path node js binary.
1.which node
2.Then soft link node to nodejs
ln -s [the path of nodejs] /usr/bin/node
I am assuming /usr/bin is in your execution path. Then you can test by typing node or npm into your command line, and everything should work now.
Uninstall whatever node version you have
sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs
install nvm (Node Version Manager) https://github.com/creationix/nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
Now you can install whatever version of node you want and switch between the versions.
I fixed it unlinking /usr/sbin/node (which is linked to ax25-node package), then I have create a link to nodejs using this on command line
sudo ln -s /usr/bin/nodejs /usr/bin/node
Because package such as karma doesn't work with nodejs name, however changing the first line of karma script from node to nodejs, but I prefer resolve this issue once and for all
For me the fix was removing the node* packages and also the npm packages.
Then a fresh install as:
sudo apt-get install autoclean
sudo apt-get install nodejs-legacy
npm install
Problem is not in installer
replace nodejs with node or change the path from /usr/bin/nodejs to /usr/bin/node
This is the your node is not properly install, first you need to uninstall the node then install again.
To install the node this may help you
http://array151.com/blog/nodejs-tutorial-and-set-up/
after that you can install the packages easily. To install the packages this may help you
http://array151.com/blog/npm-node-package-manager/
you can create a link ln -s nodejs node in /usr/bin
hope this solves your problem.
node -v // first check it's install or not
npm -v
sudo apt install npm
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash –
sudo apt-get install nodejs
then check
node -v or node –version
npm -v or npm –version
or you can remove package.lock json file / node_modules than run npm i
I hope it'll work fine
steps : https://www.geeksforgeeks.org/installation-of-node-js-on-linux/
Faced same issue, steps below worked for me.
Install curl on your system then run NVM installer script.
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Load the environment
source ~/.profile
Install the supported version of Node.js.
nvm install 16.15.1
Confirm the installation
node -v

Cannot install packages using node package manager in Ubuntu

NodeJS interpreter name(node) on Ubuntu has been renamed to nodejs because of a name conflict with another package. Here's what the readme. Debian says:
The upstream name for the Node.js interpreter command is "node".
In Debian the interpreter command has been changed to "nodejs".
This was done to prevent a namespace collision: other commands use
the same name in their upstream, such as ax25-node from the "node"
package.
Scripts calling Node.js as a shell command must be changed to instead
use the "nodejs" command.
However, using nodejs mucks up installing packages using npm. Package installation fails with the following error:
sh: 1: node: not found
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian
How do I make npm understand that nodejs is already installed on the system but the interpreter name is different?
TL;DR:
sudo apt-get install nodejs-legacy
First of all let me clarify the situation a bit. In summer 2012 Debian maintainers decided to rename Node.js executable to prevent some kind of namespace collision with another package. It was very hard decision for Debian Technical Committee, because it breaks backward compatibility.
The following is a quote from Committee resolution draft, published in Debian mailing list:
The nodejs package shall be changed to provide /usr/bin/nodejs, not /usr/bin/node. The package should declare a Breaks: relationship with
any packages in Debian that reference /usr/bin/node.
The nodejs source package shall also provide a nodejs-legacy binary package at Priority: extra that contains /usr/bin/node as a symlink to
/usr/bin/nodejs. No package in the archive may depend on or recommend
the nodejs-legacy package, which is provided solely for upstream
compatibility. This package declares shall also declare a Conflicts:
relationship with the node package.
<...>
Paragraph 2 is the actual solution for OP's issue. OP should try to install this package instead of doing symlink by hand. Here is a link to this package in Debian package index website.
It can be installed using sudo apt-get install nodejs-legacy.
I have not found any information about adopting the whole thing by NPM developers, but I think npm package will be fixed on some point and nodejs-legacy become really legacy.
Try linking node to nodejs. First find out where nodejs is
whereis nodejs
Then soft link node to nodejs
ln -s [the path of nodejs] /usr/bin/node
I am assuming /usr/bin is in your execution path. Then you can test by typing node or npm into your command line, and everything should work now.
You can also install Nodejs using NVM or Nodejs Version Manager There are a lot of benefits to using a version manager. One of them being you don't have to worry about this issue.
Instructions:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
Once the prerequisite packages are installed, you can pull down the nvm installation script from the project's GitHub page. The version number may be different, but in general, you can download and install it with the following syntax:
curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh
This will download the script and run it. It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.
To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:
source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can type:
nvm ls-remote
. . .
v0.11.10
v0.11.11
v0.11.12
v0.11.13
v0.11.14
As you can see, the newest version at the time of this writing is v0.11.14. You can install that by typing:
nvm install 0.11.14
Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:
nvm use 0.11.14
When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:
node -v
The comeplete tutorial can be found here
Install nvm first using:
curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
Run command
source ~/.profile
Now run this and this will show will all installed or other versions of packages:
nvm ls-remote
Installed packages will be in green. Install whatever version you want:
nvm install 6.0.0
Check where is not installed:
which node
Check current version:
node -v
n=$(which node);
n=${n%/bin/node};
chmod -R 755 $n/bin/*;
sudo cp -r $n/{bin,lib,share} /usr/local
sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs
sudo apt-get install nodejs-legacy
source ~/.profile
Combined the accepted answer with source ~/.profile from the comment that has been folded and some clean up commands before. Most likely you will also need to sudo apt-get install npm after.
for me problem was solved by,
sudo apt-get remove node
sudo apt-get remove nodejs
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
alias node=nodejs
rm -r /usr/local/lib/python2.7/dist-packages/localstack/node_modules
npm install -g npm#latest || sudo npm install -g npm#latest
Here's another approach I use since I like n for easy switching between node versions.
On a new Ubuntu system, first install the 'system' node:
curl -sL https://deb.nodesource.com/setup | sudo bash -
Then install n module globally:
npm install -g n
Since the system node was installed first (above), the alternatives system can be used to cleanly point to the node provided by n. First make sure the alternatives system has nothing for node:
update-alternatives --remove-all node
Then add the node provided by n:
update-alternatives --install /usr/bin/node node /usr/local/bin/node 1
Next add node provided by the system (the one that was installed with curl):
update-alternatives --install /usr/bin/node node /usr/bin/nodejs 2
Now select the node provided by n using the interactive menu (select /usr/local/bin/node from the menu presented by the following command):
update-alternatives --config node
Finally, since /usr/local/bin usually has a higher precedence in PATH than /usr/bin, the following alias must be created (enter in your .bashrc or .zshrc) if the alternatives system node is to be effective; otherwise the node installed with n in /usr/local/bin takes always precedence:
alias node='/usr/bin/node'
Now you can easily switch between node versions with n <desired node version number>.
On Linux Mint 17, I tried both solutions (creating a symlink or using the nodejs-legacy package) without success.
The only thing that finally worked for me was using the ppa from Chris Lea:
sudo apt-get purge node-*
sudo apt-get autoremove
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
This installed node version 10.37 and npm 1.4.28. After that, I could install packages globally.
As other folks already mention, I will suggest not to use "sudo apt-get" to install node or any development library. You can download required version from https://nodejs.org/dist/v6.9.2/ and setup you own environment.
I will recommend tools like nvm and n, to manage you node version. It is very convenient to switch and work with these modules.
https://github.com/creationix/nvm
https://github.com/tj/n
Or write basic bash to download zip/tar, extract move folder and create a soft link. Whenever you need to update, just point the old soft link to new downloaded version.
Like I have created for my own, you can refer:
https://github.com/deepakshrma/NodeJs-4.0-Reference-Guide/blob/master/nodejs-installer.sh
#Go to home
cd ~
#run command
#New Script
wget https://raw.githubusercontent.com/deepakshrma/NodeJs-4.0-Reference-Guide/master/nodejs-installer.sh
bash nodejs-installer.sh -v lts
#here -v or --version can be sepecific to 0.10.37 or it could be latest/lts
#Examples
bash nodejs-installer.sh -v lts
bash nodejs-installer.sh -v latest
bash nodejs-installer.sh -v 4.4.2
Simple solution from here
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash --
sudo apt-get install nodejs
You can specify version by changing setup_x.x value, for example to setup_5.x
Your System is not able to detect the path node js binary.
1.which node
2.Then soft link node to nodejs
ln -s [the path of nodejs] /usr/bin/node
I am assuming /usr/bin is in your execution path. Then you can test by typing node or npm into your command line, and everything should work now.
Uninstall whatever node version you have
sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs
install nvm (Node Version Manager) https://github.com/creationix/nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
Now you can install whatever version of node you want and switch between the versions.
I fixed it unlinking /usr/sbin/node (which is linked to ax25-node package), then I have create a link to nodejs using this on command line
sudo ln -s /usr/bin/nodejs /usr/bin/node
Because package such as karma doesn't work with nodejs name, however changing the first line of karma script from node to nodejs, but I prefer resolve this issue once and for all
For me the fix was removing the node* packages and also the npm packages.
Then a fresh install as:
sudo apt-get install autoclean
sudo apt-get install nodejs-legacy
npm install
Problem is not in installer
replace nodejs with node or change the path from /usr/bin/nodejs to /usr/bin/node
This is the your node is not properly install, first you need to uninstall the node then install again.
To install the node this may help you
http://array151.com/blog/nodejs-tutorial-and-set-up/
after that you can install the packages easily. To install the packages this may help you
http://array151.com/blog/npm-node-package-manager/
you can create a link ln -s nodejs node in /usr/bin
hope this solves your problem.
node -v // first check it's install or not
npm -v
sudo apt install npm
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash –
sudo apt-get install nodejs
then check
node -v or node –version
npm -v or npm –version
or you can remove package.lock json file / node_modules than run npm i
I hope it'll work fine
steps : https://www.geeksforgeeks.org/installation-of-node-js-on-linux/
Faced same issue, steps below worked for me.
Install curl on your system then run NVM installer script.
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Load the environment
source ~/.profile
Install the supported version of Node.js.
nvm install 16.15.1
Confirm the installation
node -v

Can't use NVM from root (or sudo)

I've noticed that my application uses different version of NodeJS when running from sudo.
$ node -v
v0.10.23
$ sudo node -v
v0.11.8-pre
This v0.11.8-pre caused me some problems, so I definitely don't want to use it, but I can't change it for root.
$ sudo nvm use v0.10.23
sudo: nvm: command not found
I've tried to install nvm from root user, but got error "NVM already installed", but still nvm not found when running from sudo. What is my problem?
My solution is to create symbolic links from the versions of node and npm I'm using to /usr/local/bin:
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"
This makes npm and node available to all users.
The below list of commands (source: digitalocean) seems to fix the problem
WARNING!!!! In some circumstances, these commands can break your system! Make sure you know what do these command do!!! related
n=$(which node); \
n=${n%/bin/node}; \
chmod -R 755 $n/bin/*; \
sudo cp -r $n/{bin,lib,share} /usr/local
The above command is a bit complicated, but all it's doing is copying whatever version of node you have active via nvm into the /usr/local/ directory (where user installed global files should live on a linux VPS) and setting the permissions so that all users can access them.
The fundamental reason is because nvm is not a real program. It's a bash function that gets loaded in the user's .profile, .bashrc, or ... So sudo doesn't automatically pick it up from the $PATH like most other programs.
An alternative node version manager is n: https://github.com/tj/n . That is a real program, so sudo will pick it up via the $PATH without any hacks (as long as sudo has /usr/local/bin in its $PATH).
sudo npm install -g n # install 'n' globally
which n # should be /usr/local/bin/n
sudo n lts # need sudo to switch node versions
node --version # v6.10.0
sudo node --version # v6.10.0
Your problem is, that nvm is not in the path when you use sudo.
So type
$ which nvm
and the result will be something like
/home/abc/mynvm/nvm
Try again now with sudo:
sudo /home/abc/mynvm/nvm use v0.10.23
I assume you then run into the issue that the root user can't find the 0.10.13-version, but lets see the next error message...
According to README
When using nvm you do not need sudo to globally install a module with npm -g, so instead of doing sudo npm install -g grunt, do instead npm install -g grunt
Need sudo npm?
In my case, I need to sudo npm run start which needs the access to some file requiring root access. According to this issue,
You don't use sudo. You should instead chmod/chown the file so that the user that has nvm has access to the file;.
In sum
The maintainer of nvm strongly believe we don't need to sudo :P
I had your problem too. Finally I have worked around it. Here is my solution:
Uninstall nvm and nodejs. Here are some helpful links: Uninstallation of nvm. If you installed nodejs using apt-get, you can uninstall it with the command apt-get purge nodejs.
Install a global nvm. See this page : nvm global. As it says, "Standard nvm has known difficulties working in multi-user or rooted environments."
After restarting your terminal, you can run the command sudo nvm ls.
$ sudo bash -ic "nvm use stable; npm -v"
Now using node v6.3.1 (npm v3.10.3)
3.10.3
By extending #SimpleJ solution I have created a useful bash script that could be used to link all binaries from actual nvm bin dir to /usr/local/bin:
#!/bin/bash
. ~/.nvm/nvm.sh
DIR=$NVM_DIR/versions/node/$(nvm version)/bin/*
DEST=/usr/local/bin
for filename in $DIR; do
filename=$(basename $filename)
DEST_FILE=$DEST/$filename
echo "Copying $filename to $DEST_FILE"
sudo ln -sf "$NVM_DIR/versions/node/$(nvm version)/bin/$filename" "$DEST_FILE"
done
I have tried the same on my machine where I have nvm as well and I have a slighlty different response:
$ sudo node --version
sudo: node: command not found
My guess is that you have installed node 0.11 outside of nvm. (Via package manager or even from source)
Therefore, running node via sudo would pick up this standalone node instead.
Does that make sense or am I mistaken?
The easiest solution to this will likely be to just hit the nvm.sh executable wherever it is.
sudo /home/ubuntu/.nvm/nvm.sh install node
This works fine for me (assuming that's the install path).
The full install procedure would look like
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
export NVM_DIR="/home/ubuntu/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
And then you can run the command above to hit the newly installed nvm.sh
I wanted to just install latest node-js from NVM API, without going for additional packages-purged versions. So I was looking to SUDO nvm install-latest-npm. Mb this will work for you - it definetely worked for me without installing/removing any apts. (Obviously change YOUR_USER_DIRECTORY for something suitable on ur system)
sudo /home/YOUR_USER_DIRECTORY/.nvm/nvm.sh | nvm install-latest-npm
Install nvm globally with
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sudo bash

On EC2: sudo node command not found, but node without sudo is ok

I have just installed nodejs on a new EC2 micro instance.
I installed it normally, ./configure -> make -> sudo make install.
Problem: When I run "node" under ec2-user, it runs perfectly. When I run "sudo node", it fails.
I found out that node is in:
[ec2-user#XXXX ~]$ whereis node
node: /usr/local/bin/node /usr/local/lib/node
and the current path is
[ec2-user#XXXX ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/opt/aws/bin:/home/ec2-user/bin
but, the sudo path is
[root#ip-10-112-222-32 ~]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin
then I tried to edit the root PATH to include the paths to node, so "node" runs when I'm logged in as root - but it still won't work when I log in as ec2-user and run "sudo node".
I need this to install npm properfly. Any idea on how to include the node path while running "sudo node"?
Yes, it is a bit annoying but you can fix it with some links:
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
There might be more but that is all I have run across so far. Lack of node-waf will cause some npm installs to fail with a rather cryptic error message.
I added /usr/local/bin to secure_path in /etc/sudoers
$ sudo visudo
Then change this line:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
To:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
it happens because the node executable is not found in /usr/bin. So follow the steps:
find node:
whereis node
in my case: node: /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node
make a symbolic link for node:
sudo ln -s /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node /usr/bin/node
It's done!
Why not use the absolute path to node? If you planning to use an upstart script it is going to need an absolute path anyways.
sudo /usr/local/bin/node server.js
try the following:
export PATH=$PATH:/usr/local/bin
sudo node --version
You could pass full path to node executable from parent (non-sudo shell) using which command.
sudo `which node`
For me, it worked to just change ownership of node folder from root to ec2-user (logged in as ec2-user).
(Note: I created my node folder in /var/lib/)
sudo chown -R ec2-user /var/lib/node/
Then
npm install mongojs
should work fine (provided you have installed npm ok of course!)
How about using "sudo $(which node)" instead of "sudo node" ?
Here's an approach that doesn't use symlinks, or require root:
$ git clone https://github.com/joyent/node.git
$ cd node
$ mkdir ~/opt
$ export PREFIX=~/opt; ./configure
$ make
$ make install
$ echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc
Then I did:
$ git clone https://github.com/isaacs/npm.git
$ cd npm
$ make install
The benefits of not running node as root are discussed here:
http://increaseyourgeek.wordpress.com/2010/08/18/install-node-js-without-using-sudo/
Its inline with:
https://github.com/joyent/node/wiki/Installation
In my case, Node was installed without sudo prefix. So node was unavailable for the superuser that why it is not working sudo node server
Enter as root with
sudo su
and then do standard steps
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
node -e "console.log('Running Node.js ' + process.version)"
This is what I did to solve a similar issue. Note: I had installed node via snap.
Step 1: Install node via snap
sudo snap install node --classic
Step 2: Find where node has been installed
whereis node
In my case
/snap/bin/node.npm
/snap/bin/node.npx
/snap/bin/node.yarn
/snap/bin/node
/snap/bin/node.yarnpkg
Step 3: Create symbolic links to node, npm, npx and yarn
sudo ln -s /snap/bin/yarn /usr/bin/yarn
sudo ln -s /snap/bin/node /usr/bin/node
sudo ln -s /snap/bin/npm /usr/bin/npm
Finally node is accessible for all users, even sudo group
sudo node
I tried everything mentioned in the above answers but nothing worked. Probably because of my lack of understanding of concepts related to sym links. I fixed the issue by not using nvm.
I just created a new ec2 instance and didn't install nvm. Instead I installed node and npm like so:
sudo apt update
sudo apt install nodejs npm
And this did it for me.
If you have installed node environment already, paste these command in you r instance and it'll link your node to your user directory. And you will be able to use node with sudo.
sudo ln -s "$(which node)" "/usr/local/bin/node"
sudo ln -s "$(which npm)" "/usr/local/bin/npm"
I don't know if this is the right way, but this is what i did...
sudo su
export PATH=$PATH:/home/ec2-user/local/node/bin
curl http://npmjs.org/install.sh | sh
chown -R ec2-user /home/ec2-user/local/node
exit
This installed npm, and I can now install any packages I want.

Resources