在 nvm 更改节点版本后,Powershell 无法识别 npm

标签 powershell npm nvm

我的 powershell 脚本有问题。我想构建一个基于节点 10.17.0 的项目并将结果复制到另一个基于节点 8.11.4 的项目中并运行该项目。

   cd $PathToWebLibs

   Write-Host "..........Switching to node v10.17.0.........." -ForegroundColor Magenta
   nvm use 10.17.0

   Write-Host "..........Building WebLibs.........." -ForegroundColor Magenta
   npm run build_lib

   Write-Host "..........Copying files from ($PathToWebLibs\dist\rsp\core-ui) to ($PathToSFP\node_modules\@rsp) .........." -ForegroundColor Magenta

   cp -Recurse -Force ($PathToWebLibs + "\dist\rsp\core-ui") ($PathToSFP + "\node_modules\@rsp")

   cd $PathToSFP

   Write-Host "..........Switching to node v8.11.4.........." -ForegroundColor Magenta
   nvm use 8.11.4


   Write-Host "..........Starting SFP.........." -ForegroundColor Magenta
   npm run start

问题是当 nvm 更改节点 npm 的版本时无法识别。当我手动执行时,只需一个接一个地输入命令即可。

我可以补充说系统环境路径设置正确。我查过了。 enter image description here

最佳答案

nvm旨在由您的 shell 在进程中运行,它仅支持与 POSIX 兼容的 shell,例如 bash,而不支持 PowerShell:

nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

在类 Unix 平台和可能的 WSL 上,考虑 Node.js 版本管理器 n作为替代方案,它不依赖于修改当前 shell 的环境。

  • n-install允许直接从 GitHub 安装 n;例如:
    curl -L https://git.io/n-install |庆典
  • 但是,这种特殊的安装方法(包括安装 Node.js 本身)目前需要在 PowerShell 中进行额外配置(在您的 PowerShell $PROFILE 文件中,或者通过Windows 上的注册表):$HOME/n/bin 必须添加到 $env:Path,并且 $env:PREFIX 必须设置为$HOME/n(如果您从 WSL 外部运行 PowerShell,则相应地调整 WSL 的路径)。

关于在 nvm 更改节点版本后,Powershell 无法识别 npm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60844828/

相关文章:

windows - 如何使用 CMD 或 Powershell 打开 'This PC'?

powershell - 获取执行脚本的当前目录,而不是脚本的当前执行目录

PowerShell AST 修改和范围

npm - 我应该提交yarn.lock 和package-lock.json 文件吗?

Docker 容器退出 : segmentation fault, errno 139,退出状态 139

node.js - gulp-front-matter 把我的前文放在哪里?

javascript - 无法让 ESM 与 Knex CLI v0.21.5 一起使用

node.js - 尝试运行示例的 gulp 问题 - 'The super constructor to ` 继承`不能为空或未定义。

windows - 使用 powershell 删除文件夹上的所有 ACL

nvm - 使用 NVM 为特定的主要版本安装最新的 NodeJS