Exploring package.json
suggest changeA package.json file, usually present in the project root, contains metadata about your app or module as well as the list of dependencies to install from npm when running npm install.
To initialize a package.json type npm init in your command prompt.
To create a package.json with default values use:
npm init --yes
# or
npm init -y
To install a package and save it to package.json use:
npm install {package name} --save
You can also use the shorthand notation:
npm i -S {package name}
NPM aliases -S to --save and -D to --save-dev to save in your production or development dependencies respectively.
The package will appear in your dependencies; if you use --save-dev instead of --save, the package will appear in your devDependencies.
Important properties of package.json:
{
"name": "module-name",
"version": "10.3.1",
"description": "An example module to illustrate the usage of a package.json",
"author": "Your Name <your.name@example.org>",
"contributors": [{
"name": "Foo Bar",
"email": "foo.bar@example.com"
}],
"bin": {
"module-name": "./bin/module-name"
},
"scripts": {
"test": "vows --spec --isolate",
"start": "node index.js",
"predeploy": "echo About to deploy",
"postdeploy": "echo Deployed",
"prepublish": "coffee --bare --compile --output lib/foo src/foo/*.coffee"
},
"main": "lib/foo.js",
"repository": {
"type": "git",
"url": "https://github.com/username/repo"
},
"bugs": {
"url": "https://github.com/username/repo/issues"
},
"keywords": [
"example"
],
"dependencies": {
"express": "4.2.x"
},
"devDependencies": {
"assume": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
},
"peerDependencies": {
"moment": ">2.0.0"
},
"preferGlobal": true,
"private": true,
"publishConfig": {
"registry": "https://your-private-hosted-npm.registry.domain.com"
},
"subdomain": "foobar",
"analyze": true,
"license": "MIT",
"files": [
"lib/foo.js"
]
}
Information about some important properties:
name
The unique name of your package, written in lowercase. This property is required. npm will not install the package without it.
- The name must be less than or equal to 214 characters.
- The name can’t start with a dot or an underscore.
- New packages must not have uppercase letters in the name.
version
The version of the package is specified by Semantic Versioning (semver). Which assumes that a version number is written as MAJOR.MINOR.PATCH and you increment the:
- MAJOR version when you make incompatible API changes
- MINOR version when you add functionality in a backwards-compatible manner
- PATCH version when you make backwards-compatible bug fixes
description
The description of the project. Try to keep it short and concise.
author
The author of this package.
bin
An object which is used to expose binary scripts from your package. The object assumes that the key is the name of the binary script and the value a relative path to the script.
This property is used by packages that contain a CLI (command line interface).
scripts
An object of extra npm commands. The key is the command name and the value is the shell command. Run one with npm run {command name}. prepublish also runs on a local npm install. Use prepublishOnly when the script should run only before npm publish.
A package installed locally exposes its bin names on PATH inside npm scripts. From a normal shell, the executable is ./node_modules/.bin/mocha, not ./node-modules/.bin/mocha.
main
The main entry point of the package. require('{module name}') loads this file.
Requiring the main file should not have side effects. It should not start an HTTP server or connect to a database. Export a function, such as exports.init = function () {}, and let the application call it.
keywords
An array of keywords which describe your package. These will help people find your package.
devDependencies
Dependencies used only to develop and test the module. npm install installs them unless NODE_ENV is production (or you pass --omit=dev). To install them anyway, use npm install --include=dev.
peerDependencies
If you are using this module, then peerDependencies lists the modules you must install alongside this one. For example, moment-timezone must be installed alongside moment because it is a plugin for moment, even if it doesn’t directly require("moment").
preferGlobal
preferGlobal is deprecated. It only warned that the package prefers npm install -g. It did not change install behavior. Packages with a CLI should document the global install. They should not set this field.
In all other situations you should NOT use this property.
publishConfig
The publishConfig is an object with configuration values that will be used for publishing modules. The configuration values that are set override your default npm configuration.
A common use is to publish to a private registry. Set publishConfig.registry to that registry's URL.
files
This is an array of all the files to include in the published package. Either a file path or folder path can be used. All the contents of a folder path will be included. This reduces the total size of your package by only including the correct files to be distributed. This field works in conjunction with a .npmignore rules file.