我的 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 的版本时无法识别。当我手动执行时,只需一个接一个地输入命令即可。
最佳答案
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/