2

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
}

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.