Error Executing NPM command In VSTS CI Build - npm

I'm new to CI Builds in VSTS and as a result, having a struggle with a specific npm task.
In the Build process, after the first five tasks execute, I use an npm task with a custom command. This command executes webpack script defined in my package.json
npm run prod
"prod": "webpack --env.NODE_ENV=production --config webpack.config.js --progress"
When this task runs, it errors with the following message
Error: Npm failed with return code: 1
Thoughts? Suggestions? This is trying me a little insane, I'm sure there's a simple solution :)

Replace npm run prod to run prod in command and arguments box.

Related

NPX command not running in Azure pipeline

I need your help
I was trying to run my cypress test cases which I generally run using command
"npx cypress run"
so I tried to have the same command in tasks when I created Azure pipeline
after NPM Install
I even tried installing npx via
npm task and custom command "npm install nx"
and this causing the below issue so can anyone suggest to me how to proceed in this case
"##[warning]Couldn't find a debug log in the cache or working directory
##[error]Error: Npm failed with return code: 1
"
From the error screenshot, it shows that you are using the Npm Task and running the command: npm npm install nx.
The command is invalid.
To solve this issue, you need to remove the npm in the NPM task -> Command and arguments .
Refer to the following sample:
YAML Pipeline:
- task: Npm#1
displayName: 'npm custom'
inputs:
command: custom
verbose: false
customCommand: 'install nx'
Classic Pipeline:

Why does package.json script behave differently than identical terminal command

In my npm project, in my package.json file, I have the following lines of code:
"scripts": {
"build": "webpack"
},
While in my terminal, if I run npm webpack, I get the error message:
Unknown command: "webpack"
But if I run npm run build, I get a prompt from webpack saying I need webpack-cli... so the command is obviously recognized.
I'm confused about the different behavior of these two commands. In this case, isn't running npm run build identical to running npm webpack in my terminal? Why does one command fail and one succeed? What is actually happening when I run npm run build?
If we look at the documentation,
Environment
Package scripts run in an environment where many pieces of information are made available regarding the setup of npm and the current state of the process.
path
If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.
Maybe this is the reason webpack is not recognized by the command line.

Azure DevOps - npm invisible

I have a strange problem with a release pipeline in Azure DevOps.
I have two tasks:
1) Install packages
2) Run npm using the above packages
All seems to be easy so what I do is:
1) Command line task
npm install -g mkdirp 1.0.3
npm install -g newman
npm install -g newman-reporter-junitfull
2) PowerShell task
$(newman run $collection -e $environment --env-var "x=$(x)" -r junit --reporter-junit-export $resultFile)
This all worked fine until today. I tried a lot, but nothing works.
The error I have is:
newman : The term 'newman' 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.
Did anyone have a similar issue?
I ran above npm install scripts in a Command line task, and found only the first npm install command got executed. And my following powershell task to run newman command failed with above error message too. You can check the log of Command line task to see if it is the same issue.
If you want to report this issue, you can report a problem here.
The workaround i found is to run the npm install commands in a Powershell task too, do not use Command line task. Then the npm install commands will all get executed and the following powershell task can run successfully.
Or you can separate above three npm install commands into three Command line tasks to install them separately, which will make sure the newman library is installed.

Mocha command is not found after adding in environment Variables in CodeBuild

In my buildspec.yml file I have a post-build command that runs my mocha tests:
npm run mochatest
That is something I have set in package.json as follows:
"scripts": {
"mochatest": "mocha --timeout 30000 test/functional_api_crud.js"
},
CodeBuild runs and it starts mocha and then I had a test failure because an environment variable I used in my Node.js code was not set. So, I went into the advanced settings of CodeBuild and added in the needed environment variables. Now when the run happens I get an error that mocha cannot be found! The error lines are:
[Container] 2017/12/28 19:24:29 Running command npm run mochatest
newswatcher#0.0.1 mochatest /codebuild/output/src251232826/src
mocha --timeout 30000 test/functional_api_crud.js
sh: 1: mocha: not found
npm ERR! Please include the following file with any support request:
npm ERR! /codebuild/output/src251232826/src/npm-debug.log
This started happening after I added in my own environment variables! Did some other environment variable get upset because I did this?
It turns out that I had set the NODE_ENV environment variable to production and thus, an npm install does not bring in my devDependencies modules!

What is the difference between yarn run and npm start?

Is yarn run intended to be the equivalent of npm start?
It seems yarn run start is the equivalent of npm start, which runs the script inside the start field of the script field in package.json
Few things to understand:
npm: run command is mandatory to execute user defined scripts.
yarn: run command is not mandatory to execute user defined scripts.
start command is not a user defined script name, so you may not need to specify run command to execute it.
So, all the below commands work similar!
npm start
npm run start
yarn start
yarn run start
If you have a user defined script named 'app':
npm app (Does not work!)
npm run app (Works!)
yarn app (Works!)
yarn run app (Works!)
Note: By default start runs node server.js in case not explicitly defined.
npm start is a shortcut for npm run start
Now in terms of running scripts from package.json, all these are equivalent:
npm run start
npm start
yarn run start
yarn start
npm run myscript
npm myscript this is an error
yarn run myscript
yarn myscript
This is because run is not mandatory command for yarn, but it is for npm.
Bonus
npr start - OK
npr myscript - OK
Put this file somewhere in PATH, eg. %localappdata%\Programs\Git\cmd
npr.cmd
npm run %*
yarn run is similar to npm run, they can be used to run the scripts in package.json.
For npm, you can leave out run when run the npm lifecycle scripts (test, start, restart, and stop), but these scripts maybe have extra effect. For example, npm start will run node server.js if the "scripts" object does not define a "start" property in package.json. see the doc npm run. You can't run script with other name leaving out run.
For yarn, you can leave out run for all the scripts in package.json, but if your script name is same as yarn built-in cli commands, the built-in cli commands will have preference over your scripts. doc yarn run.
So the best way to run scripts in package.json: never leave out run.