Setting up a task in package.json - node.js

I am trying to setup a task in package.json
"scripts": {
"unit-test": "mocha './test/unit/**/*.spec.js'",
}
However when I run
npm run unit-test
the following errors are thrown in the console at the end of the run:
npm ERR! Darwin 16.6.0
npm ERR! argv "/Users/shreya.vakil/.nvm/v6.9.5/bin/node" "/Users/d.bubble/.nvm/v6.9.5/bin/npm" "run" "unit-test"
npm ERR! node v6.9.5
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! myapp#0.0.1-SNAPSHOT unit-test: `mocha './test/unit/**/*.spec.js'`
npm ERR! Exit status 1
However, if I change the command to
"scripts" : {
"unit-test": "mocha './test/unit/**/*.spec.js'; exit 0",
}
this error is not thrown. However, I am not sure if this is the right approach.

The original behavior is correct, if a little ugly - if your tests fail, Mocha returns an exit code of 1, which is technically an error. Your second example makes the output prettier, but it'll cause you issues if you try to integrate your build scripts into other tools (for example, Travis), as they won't be able to detect when your tests fail.
The best solution to this would be to get rid of the exit 0 and upgrade your version of NPM - the output was made much shorter in v4.6.1. You can do this by running the following command:
npm install npm -g

Related

Why can't npm start?

I got a new notebook, so I install all languages I use. I use nodejs v10.16.
I had an Angular code which I wanted to run on the new notebook. The version of the node was the same. However, when I wrote in the command line npm start, I got these message:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! <my-project>-client#0.0.0 start: `ng serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the <my-project>-client#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Why did it happend? I install node_modules before starting and it ran on my earlier notebook.
How should I solve this problem?
Make sure you're running the start script from the same location as your package.json file.
Check the format of package.json scripts object, ensuring the script name is correct and denoting where the script/object ends with a comma.
"scripts": {
"start": "react-scripts start"
},
Ensure the app/module your start script is calling is installed/installed correctly.
Use the full npm command to run your start script:
npm run start
If all else fails, rename your node_modules to node_modules_old, check your package.json file has all your required dependencies and run npm install from the same directory to generate a new node_modules folder before trying to run the script again with npm run start.

Why is Ava js not showing error logging locally, only in ci

I have a failing ava test that should error out like this:
$ npm run test
> fetch_courses#1.0.0 test /home/travis/build/********/fetch_courses
> tsc && ava
Uncaught exception in test/fetchTerms.test.ts
Error: Cannot find module '../config.js'
Require stack:
- /home/travis/build/********/fetch_courses/src/fetchTerms.ts
- /home/travis/build/********/fetch_courses/test/fetchTerms.test.ts
- /home/travis/build/********/fetch_courses/node_modules/ava/lib/worker/subprocess.js
› - node_modules/ava/lib/worker/subprocess.js
› src/fetchTerms.ts:2:1
› Object.<anonymous> (src/fetchTerms.ts:139:4)
› Module.m._compile (node_modules/ts-node/src/index.ts:858:23)
› require.extensions.<computed> (node_modules/ts-node/src/index.ts:861:12)
✖ test/fetchTerms.test.ts exited with a non-zero exit code: 1
─
1 uncaught exception
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fetch_courses#1.0.0 test: `tsc && ava`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the fetch_courses#1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/travis/.npm/_logs/2020-07-21T21_30_16_127Z-debug.log
The command "npm run test" exited with 1.
cache.2
store build cache
which only happens in my travis build.
When I freshly clone [my repository][1] and run npm install and npm run test, which is exactly what happens in travis, this is the logging i see.
$ npm run test
> fetch_courses#1.0.0 test /mnt/c/Users/********/Projects/fetch_courses
> tsc && ava
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fetch_courses#1.0.0 test: `tsc && ava`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the fetch_courses#1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /mnt/c/Users/********/.npm/_logs/2020-07-21T22_06_02_028Z-debug.log
So you can see that I'm missing the entire Ava output/error logging.
I don't know what else I can do, but maybe it's some sort of configuration issue? The repository is linked so feel free to poke around and/or try to reproduce.
Couldn't find an answer since it seems like the issue is local to my IDE only, so my workaround was to run ava on the compiled javascript version of the tests. Logging worked there, for whatever reason.

error when trying to git clone and launch locally

i've cloned a few projects successfully, but am having trouble with the latest one...see below
i've done the following (and have repeated the process in deleting and starting from scratch a few times now)
- git clone, cd / "project x", yarn install
BUT when i try to "yarn start", i run into errors...
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app#0.1.0 open:src: babel-node tools/server.js development
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the trading-app#0.1.0 open:src script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
*note: i'm a little new at this, so any step by step guide would be appreciate!!

I am getting different results running a script with npm than calling the command directly - Node

I am setting up my environment to lint ES6. I installed eslint and can see it in my node_modules. In my node_modules/bin directory, I have the command eslint.
I can run the command and point it at the directory and I get no errors:
./node_modules/.bin/eslint src/main/webapp/js/lcrf
I can see all of the linting errors that I need to fix.
I have also added the command into my package.json:
"scripts": {
"lint": "eslint src/main/webapp/js/lcrf"
},
Now I try to run the command with npm run lint. It lints my files and I get the same number of linting errors, but then node errors out. Here is my stacktrace:
npm ERR! Darwin 14.3.0
npm ERR! argv "node" "/usr/local/bin/npm" "run" "lint"
npm ERR! node v0.12.2
npm ERR! npm v2.7.4
npm ERR! code ELIFECYCLE
npm ERR! lcrf#0.0.0 lint: `eslint src/main/webapp/js/lcrf`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the lcrf#0.0.0 lint script 'eslint src/main/webapp/js/lcrf'.
npm ERR! This is most likely a problem with the lcrf package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! eslint src/main/webapp/js/lcrf
npm ERR! You can get their info via:
npm ERR! npm owner ls lcrf
npm ERR! There is likely additional logging output above.
What would cause this to happen? What is the difference in the 2 ways that I am running the command?
npm is freaking out over the non-zero return code from eslint. If you want lint errors to be an indication of "whoa, that's not supposed to happen, red alert, something is really wrong if this got published" then that's what you've got.
If you don't use the exit code (say, to stop subsequent build steps) and you just want the output to be just the eslint output and no subsequent npm freak out, use this in your package.json:
"lint": "eslint src/main/webapp/js/lcrf || exit 0"
Note this in "Best Practices" section of the npm scripts docs:
Don't exit with a non-zero error code unless you really mean it. Except for uninstall scripts, this will cause the npm action to fail, and potentially be rolled back. If the failure is minor or only will prevent some optional features, then it's better to just print a warning and exit successfully.

npm error ELIFECYCLE while running the test

I am using mocha-phantomjs setup for unit testing. I have following package.json scriot to run the tests.
"scripts": {
"test": "npm run testFlickr",
"testFlickr": "mocha-phantomjs ./test/FlickrTest.html"
}
This run ok in browser. And when I run the command npm testin cmd, the test run alright but it also gives following error
3 passing (5s)
6 failing
npm ERR! flickr-test# testFlickr: `mocha-phantomjs ./test/FlickrTest.html`
npm ERR! Exit status 6
npm ERR!
npm ERR! Failed at the flickr-test# testFlickr script.
npm ERR! This is most likely a problem with the flickr-test package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! mocha-phantomjs ./test/FlickrTest.html
npm ERR! You can get their info via:
npm ERR! npm owner ls flickr-test
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7600
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "testFlickr"
npm ERR! cwd C:\Users\user\Desktop\test
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! C:\Users\user\Desktop\test\npm-debug.log
npm ERR! not ok code 0
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
Please can anyone tell me how can I resolve this error.
I was having this same problem, and what fixed it for me was when I run my unit tests, do NOT use
npm run test
instead use:
npm test
When running npm test it squelches ELIFECYCLE errors so you never encounter this experience.
Reference to code
Once you move a test script to a different script-name you'll start seeing ELIFECYCLE errors.
My fix is to add a exit 0 to the end of the command. For your code it would look like this:
"scripts": {
"test": "npm run testFlickr",
"testFlickr": "mocha-phantomjs ./test/FlickrTest.html; exit 0"
}
And when I run the command npm test in cmd, the test run alright
No they aren't. You have 6 failing tests. The exit code of mocha-phantomjs is equal to the number of failing tests. Run mocha-phantomjs ./test/FlickrTest.html directly and see what is failing. Realize the PhantomJS is a bit different than your browser - you may have to debug it.
Your script setup is odd. You should just have:
"scripts": {
"test": "mocha-phantomjs ./test/FlickrTest.html"
}
Then the odd node errors should go away.
This is a problem when using npm run, it has to do with Mocha exiting with code !== 0 whenever a test fails. If you are on Windows try this:
"scripts": {
"test": "npm run testFlickr || ECHO.",
"testFlickr": "mocha-phantomjs ./test/FlickrTest.html || ECHO."
}

Resources