harpal
harpal

Reputation: 476

get npm process exitcode as output in powershell

I'm new to Powershell and want to automate some process by Powershell script. Is there any way to get the status of npm run build, if it fails. Currently, if I'm not adding a condition then it sequencely runs the next commands either its success or fails.

I already added $ErrorActionPreference = "Stop" but it only works for cmdlet.

$exitcode = npm run build
#need exitcode as output to check
if ($exitcode -eq 0) {
   Remove-Item -Path $spaNodeModulePath -Force -Recurse
   mkdir $spaNodeModulePath
   xcopy $libDistPath $spaNodeModulePath /e
   Set-Location $spaPath
}

Upvotes: 1

Views: 1519

Answers (1)

user459872
user459872

Reputation: 24602

You can use $LASTEXITCODE variable which contains the exit code of the last native program that was run. So first execute the program and check the $LASTEXITCODE later.

npm run build 
if ($LASTEXITCODE -eq 0) {
    Write-Output "NPM build was successful"
}

Please note that $LASTEXITCODE is not something that user defines, It's created and maintained by the PowerShell runtime.

Upvotes: 5

Related Questions