- Node Cookbook(Third Edition)
- David Mark Clements Matthias Buus Matteo Collina Peter Elger
- 233字
- 2024-10-29 20:27:02
Using npm run scripts
Our package.json file currently has a scripts property that looks like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Let's edit the package.json file and add another field, called lint, as follows:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "standard"
},
Now, as long as we have standard installed as a development dependency of our module (refer to Installing Development Dependencies), we can run the following command to run a lint check on our code:
npm run-script lint
This can be shortened to the following:
npm run lint
When we run an npm script, the current directory's node_modules/.bin folder is appended to the execution context's PATH environment variable. This means that, even if we don't have the standard executable in our usual system PATH, we can reference it in an npm script as if it was in our PATH.
Some consider lint checks to be a precursor to tests.
Let's alter the scripts.test field, as illustrated:
"scripts": {
"test": "npm run lint",
"lint": "standard"
},
Later, we can append other commands to the test script using the double ampersand (&&) to run a chain of checks. For instance, "test": "npm run lint && tap test".
Now, let's run the test script:
npm run test
Since the test script is special, we can simply run this:
npm test