Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Act on errors in NPM commands run from PowerShell

Tags:

npm

powershell

I have an NPM command that I am running from PowerShell:

npm install process-migrator -g 
process-migrator 

This command returns an error when its not configured properly, but I cant seam to pick it up in PowerShell.

[ERROR] [2018-09-25T15:30:30.610Z] Cannot find configuration file 'configuration.json'
[INFORMATION] [2018-09-25T15:30:30.615Z] Generated configuration file as 'configuration.json', please fill in required information and retry.

Is there some way to get the error?

I have tried:

if($?) {            
   Write-Host "Process-Migrator completed sucessfully." $LASTEXITCODE           
} else {   
   Write-VstsTaskError "Process-Migrator FAILED " $LASTEXITCODE -ErrCode $LASTEXITCODE
} 
Process-Migrator completed sucessfully. 1

But $? is returning true and $LASTEXITCODE is a 1.

like image 817
MrHinsh - Martin Hinshelwood Avatar asked Mar 16 '26 23:03

MrHinsh - Martin Hinshelwood


1 Answers

You will need to capture the output of the application first:

$output = & 'process-migrator.exe' 2>&1
if ($LASTEXITCODE -ne 0)
{
    $err = $output.Where{$PSItem -match 'ERROR'}
    Write-VstsTaskError "Process-Migrator FAILED: $err" -ErrCode $LASTEXITCODE
}

Note: I made an assumption about the extension to be more granular with the execution

like image 117
Maximilian Burszley Avatar answered Mar 18 '26 22:03

Maximilian Burszley