4

I need to iteratively run an npm command and want to do it from a PowerShell task in a VSTS build step. I cannot use "npm <command>" in PowerShell and have also tried to run commands using "C:\Program Files\nodejs\npm.cmd" "<command>" and Invoke-Expression "C:\Program Files\nodejs\npm.cmd" "<command>" in the PowerShell on the VSTS hosted build agent. How can I run npm commands from a PowerShell build step?

Additionally, the <command> is a custom script from my package.json file. It does not need to be custom. I only put it there to call in a custom npm VSTS build step.

2
  • if you click on "add a build step" and go to the "package" category, there should be an npm option. does this meet your needs? Commented Jan 27, 2018 at 0:11
  • Is there a npm.exe? Do you know whether that's there during the build step? Commented Jan 27, 2018 at 2:07

2 Answers 2

3

You can call the npm command with the Start-Process and supplying the arguments through -ArgumentList switch seperated by commas

example Start-Process npm -ArgumentList "run","build" -wait the -wait switch will wait for the task to be completed.

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
2

You can call npm command directly through PowerShell task with Hosted agent.

Make sure the Working folder (package.json folder path) of PowerShell task is right.

Regarding custom script in package.json, you need to call it like npm run [script key].

2 Comments

Thanks for your help. How can I set the correct working folder path then? I run into this problem with the package.json path: ##[error]npm ERR! path D:\a\1\s\package.json npm ERR! code ENOENT npm ERR! errno -4058 npm ERR! syscall open npm ERR! enoent ENOENT: no such file or directory, open 'D:\a\1\s\package.json' The PowerShell is in the same dir as the package.json
I fixed this by: (1) calling the command from PowerShell as you suggested as npm run [script key] and (2) updating the "Working folder" reference in the advanced options of the PowerShell VSTS build task to point to where the PS modules I wrote are (I run the npm run [script key] in one of these modules). The main PS script is in the same folder as the package.json, but the PS modules with the npm run [script key] are in a child directory.

Your Answer

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