nvm: reference “default” node version path in Shell script

跟風遠走 提交于 2020-01-05 02:30:46

问题


I'm writing a daemon script for Linux Debian, and I'd need to be able to access the $NVM_BIN shell environment variable, or somehow reference the path to the current "default" Node version as set in nvm.

Several of these daemon scripts will be running on the system.

Since all these scripts can share the same Node version, I'd like to reference some variable that gets automatically updated when I install a new version of Node and define it as "default", without having to manually change the Node JS reference in each script.

Is this possible?

Thanks!


回答1:


I've finally found a way to do it.

The first line of the shell script must be:

#!/bin/bash

(part of the problems I had was related to the fact that my first line was #!/bin/sh instead)

Then, to access nvm variables or commands you must first source them with:

source <your_path>/nvm/nvm.sh

(where <your_path> needs to be replaced with the path to your nvm folder).

Now you can get the path to the node folder in two ways.

1. Through a standard nvm variable

$NVM_BIN

2. Directly "asking" to nvm

NVM_BIN=<your_path>/`nvm version`/bin/

Nice.



来源:https://stackoverflow.com/questions/32501150/nvm-reference-default-node-version-path-in-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!