How to make nvm run from within a script influence the environment of the calling shell? - nvm

When I run nvm from within a shell script, it doesn't seem to impact the environment of the calling shell:
$ node -v
v4.1.1
$ env | grep -i node
MANPATH=/home/ubuntu/.nvm/versions/node/v4.1.1/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
NVM_PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node
PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NODE_PATH=/mnt/shared/lib/node_modules
NVM_BIN=/home/ubuntu/.nvm/versions/node/v4.1.1/bin
$
$ cat test
#!/bin/bash
. ~/.nvm/nvm.sh
nvm use 0.10.40
nvm alias default 0.10.40
echo NVM_PATH=$NVM_PATH
echo MANPATH=$MANPATH
echo PATH=$PATH
echo NVM_BIN=$NVM_BIN
$ ./test
Now using node v0.10.40 (npm v1.4.28)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/home/ubuntu/.nvm/v0.10.40/lib/node
MANPATH=/home/ubuntu/.nvm/v0.10.40/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
PATH=/home/ubuntu/.nvm/v0.10.40/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_BIN=/home/ubuntu/.nvm/v0.10.40/bin
$
$ node -v
v4.1.1
$ env | grep -i node
MANPATH=/home/ubuntu/.nvm/versions/node/v4.1.1/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
NVM_PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node
PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NODE_PATH=/mnt/shared/lib/node_modules
NVM_BIN=/home/ubuntu/.nvm/versions/node/v4.1.1/bin
$
What do I need to do inside script "test" so that "node -v" will give me 0.10.40 after I run "./test" ?
Note that if I open a new terminal, and type "node -v" I get 0.10.40. But for some reason, in the shell in which I executed the "test" script I seem to be stuck with 4.1.1.

Bash scripts run in their own process context that inherits its environment from the parent process. It's not possible to change the environment of the parent. See Can a shell script set environment variables of the calling shell?
But just as your script sources nvm with . ~/.nvm/nvm.sh, you could source your script, which will execute it in the context of the parent shell:
$ node -v
v4.1.1
$ ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2#global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v4.1.1
$ source ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2#global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v0.10.40

Related

How to get new terminal windows to have node version 12 instead of 10?

I've installed node version 12 using the below commands.
$ nvm install 12 -g
# note - nvm use 12.13.1 has same effect
v12.13.1 is already installed.
Now using node v12.13.1 (npm v6.12.1)
When I type $ node -v to confirm the node version, the command line reads v12.13.1.
I have also set it as my default node version.
$ nvm alias default 12.13.1
default -> 12.13.1 (-> v12.13.1)
Yet when I open a new window I still see version 10.
$ node -v
v10.17.0
and I have to
$ nvm use 12
every time I open a new window
and then I have
$ node -v
v12.13.1
How can I have 12 be my default without doing use every time ?
This is on Ubuntu, my .bashrc includes:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
I removed npm by removing the ~/.npm folder and then reinstalled npm with:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash
and version 12 of node with
nvm install 12
node -v
This fixed it and now new terminal windows now show
node -v
v12.13.1

How to enable nvm in steps in circleci 2.0?

Here are my steps in my
steps:
-run:
name: Setup nvm and npm
command: |
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
export NVM_DIR=$HOME/.nvm
source $NVM_DIR/nvm.sh
nvm install 8.9 && nvm alias default 8.9
-run: npm install && npm run lint && npm test
The second step always fails with this error message
/bin/bash: npm: command not found
I checked .bashrc and I can see the following lines are added to the end of the file
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Circleci 2.0 invokes the step command by starting a new shell with #!/bin/bash -eo pipefail
If I starts a docker (docker run -i -t buildpack-deps:xenial) and apply the first step, and then start a new shell via #!/bin/bash -eo pipefail, I can see npm is available on the path
I am using docker for this project
version: 2
jobs:
test_main:
docker:
- image: buildpack-deps:xenial
So why does it fail in circleci 2.0 environment? How can I ensure npm will be available to step 2 from step 1?
I have tried to add [ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc" to ~/.bash_profile (in case .bashrc is not executed due to the non-interactive/non-login shell)
To reproduce the issue you can run circleci build with this .circleci/config.yml file
version: 2
jobs:
build:
docker:
- image: buildpack-deps:xenial
steps:
- run:
name: Setup nvm and npm
command: |
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
# Activate nvm
export NVM_DIR=$HOME/.nvm
touch $HOME/.nvmrc
source $NVM_DIR/nvm.sh
# Use node 8.9
nvm install 8.9 && nvm alias default 8.9
echo 8.9 > $HOME/.nvmrc
# Enable nvm in following steps
echo '[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"' >> $HOME/.bash_profile
# To fix npm install : "node-pre-gyp: Permission denied"
npm config set user 0
npm config set unsafe-perm true
npm install -g npx webpack webpack-cli jest
node --version
npm --version
- run: npm install
You will see the following error message:
====>> npm install
#!/bin/bash -eo pipefail
npm install
/bin/bash: npm: command not found
Error: Exited with code 127
Step failed
Task failed
The problem lies with these lines:
# Enable nvm in following steps
echo '[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"' >> $HOME/.bash_profile
I was hoping to source .bashrc from .bash_profile. However since the shell of circleci is non-interactive, the environment variable PS1 is blank. Hence .bashrc basically quits immediately once it is sourced, because of this line in .bashrc
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
I have to put the following lines directly in the file specified by $BASH_ENV
echo 'export NVM_DIR=$HOME/.nvm' >> $BASH_ENV
echo 'source $NVM_DIR/nvm.sh' >> $BASH_ENV
I found that changing default node by nvm is not working for my steps.
Solved by:
- run:
name: 'Install Project Node'
command: |
set +x
source ~/.bashrc
nvm install 12
NODE_DIR=$(dirname $(which node))
echo "export PATH=$NODE_DIR:\$PATH" >> $BASH_ENV
Just source /opt/circleci/.nvm/nvm.sh in the beginning of every step.

How to run "nvm" in "oh my zsh"?

In the system there is a nodejs, installed through nvm. The command is not running npm.
Console is Oh my zsh
You can use zsh-nvm or enable it yourself by adding following lines to your ~/.zshrc
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Extra:
For faster shell initialization, I use lazynvm which only loads node when needed
lazynvm() {
unset -f nvm node npm
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
}
nvm() {
lazynvm
nvm $#
}
node() {
lazynvm
node $#
}
npm() {
lazynvm
npm $#
}
Reference: Lazy load nvm for faster shell start
Switching from Bash to Oh-My-Zsh
If you already have nvm installed and you're switching from bash to oh-my-zsh you can simply open up your .zshrc file and add the nvm plugin that is included with oh-my-zsh:
Open your zsh config file.zshrc in nano with this command: nano ~/.zshrc
Scroll down to where it shows plugins=(git) and add nvm inside the parentheses to make it show as plugins=(git nvm) (separate plugins with spaces)
Press control + O (on macOS), then enter, to save, then press control + X to exit
Then open a new terminal window/tab and enter nvm ls to confirm it works. Note that you must open a new window/tab for your shell to use the newly updated .zshrc config (or enter source ~/.zshrc, etc.)
Source: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/nvm
This worked for me on Ubuntu 20.04.
Install or update nvm
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Add in your ~/.zshrc
echo 'export NVM_DIR=~/.nvm' >> ~/.zshrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' >> ~/.zshrc
Load in the current shell environment
source ~/.zshrc
Check the nvm version
nvm -v
use homebrew to install nvm
brew install nvm
edit your system configuration
vim ~/.zshrc # or vim ~/.bashrc
export NVM_DIR=~/.nvm
esc > :wq
save file
reload the configuration
source $(brew --prefix nvm)/nvm.sh
view nvm version
$ nvm --version
# 0.36.0
enjoy it.
A much easier solution is to use the nvm plugin that is shipped by default:
It also automatically sources nvm, so you don't need to do it manually
in your .zshrc
git clone https://github.com/nvm-sh/nvm.git ~/.nvm
cd ~/.nvm && git checkout v0.35.1 (current latest release)
Add nvm to your ~/.zshrc. Ex: plugins=(... nvm)
I discovered that there is a nvm plug-in shipping with oh-my-zsh (that's different from lukechilds plugin). After short inspection, I think it adds the necessary modifications to .zshrc when loading, so simply adding nvm to the plugins list in .zshrc should work as well (and it does for me).
I did not find any more details on that default nvm plugin via google so I don't know whether this is the "go-to" solution.
Add this code to .zshrc on your user directory
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
Then run this code on your terminal:
source ~/.zshrc
With Linux (Ubuntu 20.04, 22.04 and 22.10)
With your favorite editor, you edit ~/.zshrc
nano or vi ~/.zshrc
At the end of the file, you add :
# NVM
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
And then you run :
source ~/.zshrc
I strongly suggest using christophemarois' approach to lazy loading nvm (node, npm and global packages) in order to avoid slow shell starting times:
# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")
# Lazy-loading nvm + npm on node globals call
load_nvm () {
export NVM_DIR=~/.nvm
[ -s "$(brew --prefix nvm)/nvm.sh" ] && . "$(brew --prefix nvm)/nvm.sh"
}
# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[#]}"; do
eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$# }"
done

invalid command name "Queue/LTEQueue"

I`ve installed lte in ns2.35 but it gives the folowing error:
invalid command name "Queue/LTEQueue"
while executing
"Queue/LTEQueue set qos_ true "
(file "lte.tcl" line 21)
when i run lte.tcl
please hepe meto solve it
Your error: You are using a (wrong) copy of 'ns' with no LTE, or you have a failed build.
LTE, Howto ....
$ tar xvf ns-allinone-2.35_gcc482.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNSGJCZ2YzUGJDVk0
$ cd ns-allinone-2.35/
$ patch -p0 < LTE-ns235_2014-2.patch
https://drive.google.com/file/d/0B7S255p3kFXNLVlDZ29EWWxJTFk/view?usp=sharing
$ ./install
$ cd ns-2.35/
$ sudo make install ('make install' will copy the executable 'ns' to /usr/local/bin/)
$ cp ns ns235-lte ( This is your backup and the recognizable "lte ns" )
$ sudo cp ns235-lte /usr/local/bin/
$ cd ../nam-1.15/
$ sudo make install
The examples : lte-examples-0614.tar.gz https://drive.google.com/file/d/0B7S255p3kFXNRWV4Mzc0bGYtQzA/view?usp=sharing
Run some examples:
$ ns235-lte bicfixdownlink.tcl
$ ns235-lte deVacto-lte.tcl
$ ns235-lte 24_downl413.tcl
EDIT : New example package, lte-examples-06.17.tar.gz, added 24_downl413.tcl, etc. https://drive.google.com/file/d/0B7S255p3kFXNSmd4Q3h3dXp1QWc/view?usp=sharing
And ns-allinone-2.35: gt-itm updated → ns-allinone-2.35_gcc5.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNVVlxR0ZNRGVORjQ/view?usp=sharing

configure llvm fail. Host Clang must be able to find libstdcxx4.7 or newer

my project depends on llvm+clang+cmake.I use ninja to make compile simple.
my OS is : OS X Yosemite(10.10.5).here is how the error occurs.
1) get llvm&clang
$ mkdir ~/clang-llvm && cd ~/clang-llvm
$ git clone http://llvm.org/git/llvm.git
$ cd llvm/tools
$ git clone http://llvm.org/git/clang.git
$ cd clang/tools
$ git clone http://llvm.org/git/clang-tools-extra.git extra
2) install CMake and Ninja
$ cd ~/clang-llvm
$ git clone https://github.com/martine/ninja.git
$ cd ninja
$ git checkout release
$ ./bootstrap.py
$ sudo cp ninja /usr/bin/
$ cd ~/clang-llvm
$ git clone git://cmake.org/stage/cmake.git
$ cd cmake
$ git checkout next
$ ./bootstrap
$ make
$ sudo make install
3)compile ninja
$ cd ~/clang-llvm
$ mkdir build && cd build
$ cmake -G Ninja ../llvm -DLLVM_BUILD_TESTS=ON # Enable tests; default is off.
$ ninja
$ ninja install
4)config clang
$ cd ~/clang-llvm/build
$ ccmake ../llvm
after that , I get into the advanced mode (by press t)
CMAKE_CXX_COMPILER:*I change it to clang++*
CMAKE_C_COMPILER:*I change it to clang*
LLVM_ENABLE_EH and LLVM_ENABLE_RTTI: *I change it from OFF to ON*
and then I press c to configure. error happens.ERROR is :
**CMake Error at cmake/modules/HandleLLVMOptions.cmake:43 (message):
Host Clang must be able to find libstdc++4.7 or newer!
Call Stack (most recent call first):
CMakeLists.txt:358 (include)**
Any ideas?
Thanks