13

I want to run an NVM command from bash script i.e. nvm use 0.12.7. So, I have written in bash file:

#!/bin/bash
. ~/.nvm/nvm.sh
nvm use 0.12.7

And then run the command in the terminal as sudo ./script.sh (script.sh is my bash file where above code is written). It gives me the result Now using node v0.12.7. But when I check was the version activated or not, I found no affect. i.e. I ran command nvm ls and found the result as:

v0.12.0
v0.12.7

That's mean version 0.12.7 was not being activated. So, which things should I write in bash script as I can active node version from bash file.

StreetCoder
  • 9,871
  • 9
  • 44
  • 62
  • 1
    I'm not great with linux's sudo, but I assume it's because it's setting the node version for the super user, not the current user? Can you run the script without sudo? – Joseph Young Dec 22 '15 at 04:16
  • Yes I did but got same result. – StreetCoder Dec 22 '15 at 04:17
  • The only other reason which I can think of, is that it sets the nvm version within the sub-shell. No idea how to fix that – Joseph Young Dec 22 '15 at 04:22
  • I think it is because you are setting the value for the shell session in the script. Not the node version used in your current terminal. Try using the command in the answer from this post: http://stackoverflow.com/questions/24585261/nvm-keeps-forgetting-node-in-new-terminal-session nvm alias default 0.12.7 – Mike Dec 22 '15 at 04:51
  • How about `~/.nvm/nvm.sh && nvm use 0.12.7` from the current shell – sjsam Dec 22 '15 at 06:12
  • @Mike that's not my issue actually. I can do it from terminal. I just want it from shell script. Because I want to run few commands when I start my OS. So, I want to put all of the commands in one bash script as when I'll run it, all of the commands will be executed. – StreetCoder Dec 22 '15 at 06:46
  • @sjsam I tried it but no luck. It shows `Permission denied`. So, I tried by adding a `.`. However, no success – StreetCoder Dec 22 '15 at 06:52
  • If you're interested in a tutorial how to install node with nvm, check this one: http://rainsoft.io/install-node-like-a-boss-with-nvm/ – Dmitri Pavlutin Dec 28 '15 at 09:07

1 Answers1

18

One of the advantages of nvm is that you don't need to use sudo to install versions or to switch to another version. I'm not sure why you are using sudo in your nvm command.

The problem, as others have also said, is that the version is changed in a sub-shell. So the version in your "real" shell is not changed.

You can accomplish this by running your script with . (dot space) in front of it. That will make the script to be able to change stuff in your current shell.

This is my ~/bin/nvm-use-4 script:

. /usr/local/opt/nvm/nvm.sh
nvm use 4

And using it:

prawie:~$ nvm current
v0.10.29
prawie:~$ . nvm-use-4
Now using node v4.2.1
prawie:~$ nvm current
v4.2.1

If you are forced to use sudo here, I don't think it's possible to accomplish what you want, because the sudo'ed command is run in a sub-shell.

Unfortunately, you have not told use why you want to do this or what you want to accomplish. There could be better solutions to solve your problem. For example, if you want to always use a specific version of node.js when you open a new shell, you could add the following line to .profile, .bashrc or equivalent file:

nvm use 0.12.7
Joost Vunderink
  • 1,178
  • 6
  • 14
  • thanks for your variety of information. In terms of `sudo`, I tried in both with or without `sudo`. But it wasn't working althoguh I mentioned in the question with `sudo`. I can see that if I use other commands of `nvm` and run `script.sh`, it works but activating node version is not working through shell script. i.e. `nvm use 0.12.0` or something like that. – StreetCoder Dec 22 '15 at 10:05
  • 4
    It should, if you run `. script.sh`, with a dot and a space at the start. Does that help? – Joost Vunderink Dec 22 '15 at 11:42
  • it's just awesome. It really works. I knew a new thing today. Many thanks. – StreetCoder Dec 22 '15 at 11:46