Reputation: 47686
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?
Upvotes: 184
Views: 164528
Reputation: 14798
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"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npx" "/usr/local/bin/npx"
This makes npm
, npx
and node
available to all users.
Upvotes: 357
Reputation: 1651
The simplest approach would be by making PATH
variable to include your /bin
location of your NVM_DIR
. Here is the example:
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
Add this line to make PATH variable contains your binary of Node from NVM
export PATH="$NVM_DIR/versions/node/$(nvm version)/bin:$PATH"
Hope it helps.
Upvotes: 0
Reputation: 3104
I had installed nvm
and node
as an unprivileged user.
To use these as sudo, I added this to the superuser's .bashrc
file:
$ sudo bash -c 'echo $HOME/.bashrc'
/root/.bashrc
Contents to add to /root/.bashrc
(replace <your-user>
):
# Lazy load NVM/NPM/Node
lazynvm() {
unset -f nvm node npm
export NVM_DIR=/home/<your-user>/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
}
nvm() {
lazynvm
nvm $@
}
node() {
lazynvm
node $@
}
npm() {
lazynvm
npm $@
}
Upvotes: 1
Reputation: 3126
The below list of commands (source: digitalocean) seems to fix the problem
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.
Upvotes: 267
Reputation: 189
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
Upvotes: 6
Reputation: 632
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
Upvotes: 0
Reputation: 4809
When using nvm you do not need
sudo
to globally install a module withnpm -g
, so instead of doingsudo npm install -g grunt
, do insteadnpm install -g grunt
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;.
The maintainer of nvm strongly believe we don't need to sudo
:P
Upvotes: 19
Reputation: 135
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
Upvotes: 0
Reputation: 38053
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
Upvotes: 30
Reputation: 24389
Install nvm globally with
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sudo bash
Upvotes: -3
Reputation: 551
$ sudo bash -ic "nvm use stable; npm -v"
Now using node v6.3.1 (npm v3.10.3)
3.10.3
Upvotes: 8
Reputation: 1777
I had your problem too. Finally I have worked around it. Here is my solution:
apt-get purge nodejs
.After restarting your terminal, you can run the command sudo nvm ls
.
Upvotes: 8
Reputation: 3627
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...
Upvotes: 19
Reputation: 5923
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?
Upvotes: 4