如何在BASH中使用特定版本的Node/NPM,而不使用nvm?

huangapple go评论73阅读模式
英文:

How to use a specific version of Node/NPM in BASH without using nvm?

问题

我正在使用我的工作笔记本电脑,所以我没有管理员权限。我使用的是Windows 10,并使用Git BASH。

我拥有最新版本的Node,并试图使用npm中的某个包(@servicenow/cli),但它只兼容Node版本12.16.1,因此我下载了nvm-windows来安装和使用该版本。

(我最初在BASH中使用普通的nvm,但它无法连接到下载不同Node版本的源,但nvm-windows可以工作)。

我能够使用nvm-windows安装我需要的Node版本(12.16.1)。它位于C:\Users\Username\nvm\v12.16.1,如果我使用命令nvm list,它会显示在BASH中。

但是,我无法使用命令nvm use 12.16.1切换到Node 12.16.1,因为它需要提升的管理员权限。根据nvm-windows文档:

NVM for Windows通过更新符号链接并使用mklink命令来“切换”node.js的版本。符号链接被重新创建以指向应该运行的node.js版本。这个过程需要提升的管理员权限。

所以,我的问题是,是否有人知道我如何在不使用nvm-windows的情况下使用我拥有的Node版本?C:\Users\Username\nvm\v12.16.1中有一个node.exe,但当我执行它时,它只是一个Node repl(我以为我可以在那里使用npm,哈)

尽管安装了这个版本的Node,但Node和npm命令都不起作用。我的直觉告诉我,我可以使用环境变量来指向这个Node版本,以便命令在终端中工作,但我不确定该如何做。

由于没有管理员权限,我无法编辑系统环境变量,但我可以编辑我的Windows用户的用户环境变量,我认为它的工作方式类似。 (在手动安装nvm-windows时,我必须添加两个用户变量。我不确定它们的作用/用途,但我认为它是为了在终端中使nvm命令工作?)

谢谢,感激不尽。

英文:

Context: I'm using my work laptop, so I don't have admin privileges. I'm on Windows 10 using Git BASH

I had the latest version of Node and was trying to use a certain package from npm (@servicenow/cli), but it's only compatible with Node version 12.16.1, so I downloaded nvm-windows to install and use said version.

(I was originally using regular nvm with BASH but it wasn't connecting with whatever source it connects with to download different Node versions but nvm-windows does work).

I was able to use nvm-windows to install the version of Node I need (12.16.1). It's in C:\Users\Username\nvm\v12.16.1 and it shows up in BASH if I use the command nvm list.

However, I can't use the command nvm use 12.16.1 to switch to Node 12.16.1 because it requires elevated admin privileges. From nvm-windows docs:
> NVM for Windows "switches" versions of node.js by updating a symlink, using the mklink command. The symlink is recreated to point to whichever version of node.js should run. This process requires elevated administrative permissions.

So, my question is, does anyone know how I can use the version of Node I have without using nvm-windows? There's a node.exe in C:\Users\Username\nvm\v12.16.1 but when I execute it, it's just a Node repl (I thought I would be able to use npm there, ha)

Despite having this version of Node installed, the Node and npm commands don't work. My intuition tells me that I can use Environment Variables to point to this version of Node so the commands work in the terminal , but I'm not sure how I would do that, exactly.

Due to not having admin privileges, I can't edit the System environment variables, but I can edit the User environment variables for my Windows User and I think it works similarly. (I had to add two User variables during the manual installation of nvm-windows. I'm not sure what they do/what they're for exactly, but I think it's so the nvm commands work in the terminal?)

Thanks, appreciate it.

答案1

得分: 0

我明白了。我只需要编辑路径用户变量并将包含所需Node版本的目录路径连接到末尾。Node和npm命令现在可用。

  1. 打开文件资源管理器,导航到包含所需Node版本的目录
  2. 双击路径栏并复制路径
  3. 按下Windows键,编辑您的帐户的环境变量
  4. 双击Path变量
  5. 在右侧单击“编辑文本”
  6. 导航到末尾,粘贴您复制的路径,然后点击确定,再次点击确定
  7. 打开终端,运行node -v和npm -v以验证是否可用
英文:

I got it. I just needed to edit the Path User variable and concatenate the path to the directory that contains the version of Node to the end. The Node and npm commands work now.

  1. Open file explorer and navigate to the directory that contains the
    version of Node you want to use
  2. Double click on the path bar and copy the path
  3. Windows key, Edit environment variables for your account
  4. Double click on Path variable
  5. Click on "Edit text" on the right side
  6. Navigate to the end and paste the path you copied and hit Ok, hit Ok again
  7. Open terminal and do node -v and npm -v to verify it works

huangapple
  • 本文由 发表于 2023年7月13日 23:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定