• Register
499 points
‚óŹ3

Package.json must be actual json, not just javascript

 

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.

Who uses package.json file?

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”.

Mandatory Directives of package.json file

package.json file contains a number of different directives or elements. Some are mandatory and some are optional directives.

Mandatory Directives

ackage.json file contains two mandatory directives;

  1. name
  2. 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.

Optional Directives

Here key is module name and value is required module version. For example;


"dependencies": {
      "mypackage": "1.0.0",
      "async": "0.8.0",
      "express": "4.2.x"
}

Complete package.json file example:


{
"name" : "sampleapp",
"version" : "1.0.0"'
"dependencies": {
	"mypackage": "1.0.0",
	"async": "0.8.0",
	"express": "4.2.x"
}
}

Here sample app is depending on 3 other Node JS Modules: mypackage (My own Custom Module), async and express (Node JS Modules)

 

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).

 

 

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": {
   <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" : "https://github.com/journaldev/jd.git"
}

Complete package.json file example with mandatory and optional directives:


{
	"name" : "sample",
	"version" : "1.0.0"'
	"repository": {
		"type" : "git",
		"url" : "https://github.com/journaldev/jd.git"
	},
	"dependencies": {
		"mypackage": "1.0.0",
		"async": "0.8.0",
		"express": "4.2.x"
	}
}

 

1 Comment

I'm trying to grasp the significance of this topic, but it's just beyond my comprehension.