- Node Cookbook(Third Edition)
- David Mark Clements Matthias Buus Matteo Collina Peter Elger
- 403字
- 2024-10-29 20:27:02
Installing development dependencies
We usually need some tooling to assist with development and maintenance of a module or application. The ecosystem is full of programming support modules, from linting to testing to browser bundling to transpilation.
In general, we don't want consumers of our module to download dependencies they don't need. Similarly, if we're deploying a system built-in node, we don't want to burden the continuous integration and deployment processes with superfluous, pointless work.
So, we separate our dependencies into production and development categories.
When we use npm --save install <dep>, we're installing a production module.
To install a development dependency, we use --save-dev.
Let's go ahead and install a linter.
standard is a JavaScript linter that enforces an unconfigurable ruleset. The premise of this approach is that we should stop using precious time up on bikeshedding about syntax.
All the code in this book uses the standard linter, so we'll install that:
npm install --save-dev standard
If the absence of semicolons is abhorrent, we can choose to install semistandard instead of standard at this point. The lint rules match those of standard, with the obvious exception of requiring semicolons. Further, any code written using standard can be reformatted to semistandard using the semistandard-format command tool. Simply, run npm -g i semistandard-format to get started with it.
Now, let's take a look at the package.json file:
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David Mark Clements",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8"
}
}
We now have a devDependencies field alongside the dependencies field.
When our module is installed as a sub-dependency of another package, only the hsl-to-rgb-for-reals module will be installed, while the standard module will be ignored since it's irrelevant to our module's actual implementation.
If this package.json file represented a production system, we could run the install step with the --production flag, as shown:
npm install --production
Alternatively, this can be set in the production environment with the following command:
npm config set production true
Currently, we can run our linter using the executable installed in the node_modules/.bin folder. Consider this example:
./node_modules/.bin/standard
This is ugly and not at all ideal. Refer to Using npm run scripts for a more elegant approach.