What is package.json?
package.json is a plain JSON(Java Script Object Notation) text file which contains all metadata information about Node JS Project or application.
Every Node JS Package or Module should have this file at root directory to describe its metadata in plain JSON Object format.
Why that filename is “package”: because Node JS platform manages every feature as separate component. That component is also known as “Package” or “Module”.
NPM (Node Package Manager) uses this package.json file information about Node JS Application information or Node JS Package details.
package.json file contains a number of different directives or elements. It uses these directives to tell NPM “How to handle the module or package”.
package.json file contains a number of different directives or elements. Some are mandatory and some are optional directives.
Mandatory Directives
package.json file contains two mandatory directives;
- name
- version
name: It is a unique Node JS Package (Module) name or our Node JS Project name. This name should be in lower case letters. It is mandatory directive. Without this directive, we cannot install our Node JS Package by using NPM.
JSON file follows key-value pair format. Key is of JSON String type and value may be of any JSON Data type. Key and Value elements are separated by colon “:”.
Here “name” directive’s value is of JSON String type.
Example:
“name”:”mysimplename”
When we run “npm install
version: version is the Node JS Package version number. It is a Mandatory directive in package.json file. NPM uses this version number to install or uninstall or update the right package in our NODE JS Environment.
Example :
“version”:”1.0.0″
Here we need to define 3 parts of version: major, minor and patch
Both name and version directives of package.json file identifies a Node JS Package uniquely.
Example package.json file
{
"name" : "mysample",
"version" : "1.0.0"
}
Optional Directives
package.json file may contain many optional directives. We should use them only if our Node JS package really requires. We will discuss some important optional directives here.
description: It is the description of the Node JS Project or module. We need to provide brief and concise description about “What this module does”.
dependencies: Every Node JS Project or Module may dependent on other Node JS or Third Party Modules or our own Custom Modules. We should provide all those dependencies by using this “dependencies” directive.
Here key is module name and value is required module version. For example;
"dependencies": {
"mypackage": "1.0.0",
"mongodb": "3.2.0",
"express": "4.2.x"
}
Complete package.json file
{
"name" : "mysampleapp",
"version" : "1.0.0"'
"dependencies": {
"mypackage": "1.0.0",
"mongodb": "3.2.0",
"express": "4.2.x"
}
}
While defining dependencies version we can use some pattern matching syntax.
Scenario 1: Any available version with * character
"dependencies": {
"mypackage": "*"
}
It will pickup any available version of mypackage module.
Scenario 2: Range selection with ~ symbol
"dependencies": {
"mypackage": "~0.2.0"
}
It pickup any available version of mypackage module between >=0.2.0 and < 0.3.0(Exclusive).
In development mode, we always want our Node JS application to use latest available module from Node JS repository.
Scenario 3: Range selection with ^ symbol
"dependencies": {
"mypackage": "^0.2.0"
}
It will pickup any available version of mypackage module between >=0.2.0 and < 1.0.0(Exclusive).
repository: This directive is used to specify the URL where your source code is live for other people to contribute to your module or to learn your module. Its syntax is shown below.
"repository": {
<Define your URL here>
}
Here we need to define two elements:
- Type of source to which you are going to provide URL
- url
We define these two elements in key-value pairs. Here key is element name and value is either type of source or url.
For Example :
"repository": {
"type" : "git",
"url" : "http://github.com/uitut/filename.git"
}
Complete package.json file example with mandatory and optional directives:
{
"name" : "mysample",
"version" : "1.0.0"'
"repository": {
"type" : "git",
"url" : "http://github.com/uitut/filename.git"
},
"dependencies": {
"mypackage": "1.0.0",
"mongodb": "3.3.0",
"express": "4.2.x"
}
}