简体   繁体   English

nvm:在 Shell 脚本中引用“默认”节点版本路径

[英]nvm: reference "default" node version path in Shell script

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 .我正在为 Linux Debian 编写守护程序脚本,我需要能够访问$NVM_BIN shell 环境变量,或者以某种方式引用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.由于所有这些脚本可以共享相同的 Node 版本,我想引用一些在安装新版本的 Node 时自动更新的变量并将其定义为“默认”,而无需手动更改每个中的 Node JS 引用脚本。

Is this possible?这可能吗?

Thanks!谢谢!

I've finally found a way to do it.我终于找到了一种方法来做到这一点。

The first line of the shell script must be: shell 脚本的第一行必须是:

#!/bin/bash

(part of the problems I had was related to the fact that my first line was #!/bin/sh instead) (我遇到的部分问题与我的第一行是#!/bin/sh的事实有关)

Then, to access nvm variables or commands you must first source them with:然后,要访问 nvm 变量或命令,您必须首先使用以下来源获取它们:

source <your_path>/nvm/nvm.sh

(where <your_path> needs to be replaced with the path to your nvm folder). (其中<your_path>需要替换为您的 nvm 文件夹的路径)。

Now you can get the path to the node folder in two ways.现在您可以通过两种方式获取节点文件夹的路径。

1. Through a standard nvm variable 1.通过标准的nvm变量

$NVM_BIN

2. Directly "asking" to nvm 2.直接“问”到nvm

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

Nice.好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM