marc walter

Useful npm scripts and snippets

2016-04-17

Execute mocha tests without installing mocha globally

Works both on Windows and Mac
Add to package.json: (Info: Also forces mocha to print the full stack trace when errors occur).

[...]
"scripts": {
  "test": "node ./node_modules/mocha/bin/_mocha --full-trace test"
},
[...]

Then execute npm test on the command line to run all tests from the test folder.

Run istanbul code coverage and mocha tests without installing either globally

Works both on Windows and Mac
Add to package.json: (Info: Also sets the folder for the report)

[...]
"scripts": {
  "cover": "node node_modules/istanbul/lib/cli.js cover --dir test_coverage node_modules/mocha/bin/_mocha test"
},
[...]

Then execute npm run cover on the command line to generate the reports.

Get a list of globally installed packages

E.g. useful when switching between node.js versions

npm list -g --depth=0

Example output:

> npm list -g --depth=0
C:\Program Files\nodejs
+-- eslint-cli@1.1.0
+-- grunt@1.0.1
+-- gulp@3.9.1
+-- istanbul@0.4.3
+-- mocha@2.4.5
+-- nodemon@1.9.2
+-- npm@3.8.6
`-- webpack@1.13.0