Mocha command is not found after adding in environment Variables in CodeBuild - aws-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!

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.

Bitbucket pipeline Your system is missing the dependency: Xvfb

I'm trying to add cypress to the bitbucket pipeline but it tells me that I need to install Xvfb and I don't know how to keep going. This is my bitbucket.pipelines.yml
# Template NodeJS build
# This template allows you to validate your NodeJS code.
# The workflow allows running tests and code linting on the default branch.
image: node:14.15.4
pipelines:
default:
- step:
name: Build
script:
- npm install
- npm run lint
- npm run cypress:run
This is my package.json scripts
"scripts": {
"cypress:open": "cypress open",
"cypress:run": "npx cypress run --record --key xxxxxxxxxxxx"
}
and the test are running fine locally
But in the pipeline I'm getting this error:
+ npm run cypress:run
> wallet-frontend#0.1.0 cypress:run /opt/atlassian/pipelines/agent/build
> npx cypress run --record --key 70004462-62d4-42ce-b359-5bff73d8b001
It looks like this is your first time using Cypress: 6.5.0
[16:30:09] Verifying Cypress can run /root/.cache/Cypress/6.5.0/Cypress [started]
[16:30:09] Verifying Cypress can run /root/.cache/Cypress/6.5.0/Cypress [failed]
Your system is missing the dependency: Xvfb
Install Xvfb and run Cypress again.
Read our documentation on dependencies for more information:
https://on.cypress.io/required-dependencies
If you are using Docker, we provide containers with all required dependencies installed.
----------
Error: spawn Xvfb ENOENT
----------
Platform: linux (Debian - 9.13)
Cypress Version: 6.5.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! wallet-frontend#0.1.0 cypress:run: `npx cypress run --record --key xxxxxxxxx`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the wallet-frontend#0.1.0 cypress:run script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Xvfb is an in-memory display server for a UNIX-like operating system, in your case Linux (Debian - 9.13). This is a system-level dependency that cypress assumes is installed beforehand. In the case of bitbucket env, this is not installed and hence this error. This is primarily used for Display (UI) in case of headed browsers while running cypress.
There are few easy workarounds here:
Installing the dependency manually: I won't suggest this as you might miss out few more dependencies unless you thoroughly look into all the dependencies.
Run headless browsers:: Headless browsers don't use Xvfb, so won't be facing this exact error but as I said in point 1, there can be other issues. Command will become cypress run --headless
Use Cypress provided Docker images (Suggested): I can see that you are using the node:14.15.4 image for this build. Instead of that, use a cypress provided official base images for whatever node version you want to run (Look for different node version in tags). If you look into the Dockerfile, you will see that they have taken an effort to install the dependencies needed to run Cypress inside the docker (You can see Xvfb there).
Also, look for already open issues when you get stuck in cypress-related issues, they have a very active and helpful issues section. I found this same issue in there: https://github.com/cypress-io/cypress/issues/14457
Cypress provides the dependencies for CI.
Linux
Ubuntu/Debian
apt-get install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb
CentOS
yum install -y xorg-x11-server-Xvfb gtk2-devel gtk3-devel libnotify-devel GConf2 nss libXScrnSaver alsa-lib
reference: https://docs.cypress.io/guides/continuous-integration/introduction#Dependencies

Error Executing NPM command In VSTS CI Build

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.

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.