我正在尝试将NodeJS, NPM, PM2, and Yarn
安装到运行Windows的EC2机器上。我在下面编写了PowerShell脚本来完成这项工作。当我直接在EC2
机器上运行此脚本时,一切都正常。
$target="c:/omnilink/assetwhere/platform/node.msi"
function SetPathVariables {
[CmdletBinding()]
param (
[string]$PathName
)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
[Environment]::SetEnvironmentVariable('Path', $env:Path + ';'+$PathName, 'Machine')
#Removing duplicates
[Environment]::SetEnvironmentVariable('Path',(([Environment]::GetEnvironmentVariable('Path','Machine') -split ';'|Sort-Object -Unique) -join ';'),'Machine')
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
}
echo "Downloading nodeJS"
Read-S3Object -BucketName assetwhere-build-components -File $target -Key 'Components/Nodejs/node-v18.12.1-x64.msi'
echo "Installing NodeJS"
Start-Process msiexec.exe -Wait -ArgumentList '/I C:\omnilink\assetwhere\platform\node.msi /quiet'
echo "Setting Path"
SetPathVariables -PathName 'C:\Program Files\nodejs\'
echo "Verifying NodeJS Installation"
node --version
npm --version
echo "Successfully Installed NodeJS"
echo "Installing yarn"
npm install --global yarn
echo "Installing pm2"
npm install --global pm2
SetPathVariables 'C:\Users\Administrator\AppData\Roaming\npm'
echo "Verifying global packages installations"
echo "Yarn Version: "
yarn --version
echo "Pm2 Version: "
pm2 --version
当我尝试通过Jenkins
管道执行此脚本时,脚本成功安装到节点js。但是,无法安装yarn & pm2
,我可以知道为什么吗?这是我的Jenkins日志
Downloading nodeJS
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/7/2023 10:49 AM 30683136 node.msi
Installing NodeJS
Setting Path
Verifying NodeJS Installation
v18.12.1
8.19.2
Successfully Installed NodeJS
Installing yarn
added 1 package, and audited 2 packages in 920ms
found 0 vulnerabilities
Installing pm2
added 184 packages, and audited 185 packages in 23s
12 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Verifying global packages installations
Pm2 Version:
pm2 : The term 'pm2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\omnilink\configure\InstallAssetWhereAPIRequirementsScript.ps1:36 char:1
+ pm2 --version
+ ~~~
+ CategoryInfo : ObjectNotFound: (pm2:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
1条答案
按热度按时间hkmswyz61#
在我更改了windows中的
npm global path
后,默认情况下它指向C:\Users\Administrator\AppData\Roaming\npm
。这需要管理员的权限。我已经更改了
node js
目录的全局路径。我在C:/Program Files/nodejs
中创建了一个名为npm-global
的文件夹,更改了npm全局配置。更新脚本: