Monday, October 15, 2018

Beginner guide to Package.json file tutorial with examples in Nodejs Applications

In this blog post, We are going to learn package.json tutorials with examples

Package.json file 

package.json is a JSON configuration file of a nodejs and javascript based projects. which contains key and value of elements of a  Nodejs based applications. This always contains in application root folder. This will be used by npm command for build/start/testing your javascript based applications. This will help other developers to track the application dependencies and versions
This file can be created in many ways as follows
  • Angular/ReactJS/VueJS CLI automatically
  • npm init command
  • Manually create file 
package JSON file contains the following properties
  • Project metadata like name, version etc
  • Dependencies of application - dependencies,devDependencies etc
  • npm command line automated Scripts 
  • repositories,browserlist, and engine support
 package.json file is used in below Nodejs Based application types
  •  Front-end based apps using Angular, ReactJS and Vuejs etc..
  •  Backend applications using MEAN or MERN Stack

package.json file example creation 

This example can explain different ways to create a file

  • package.json file creation with CLI prompt using npm init 
  • default package.json creation with default values using npm init -yes command


B:\Workspace\blog\reactapp>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (reactapp) nodeapp
version: (1.0.0)
description: This is node based application
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to B:\Workspace\blog\reactapp\package.json:

{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "This is node based application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) yes

Example Minimal file 

This is a file created in the application root folder with a minimum configuration of an application
{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "This is node based application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
We have created a minimal package.json file
Name is the name of the application
version is the current version of the application
Description is a long text description about projects
main - this contains entry point script code of the application
Author is Name of the developer
License- license information of an application. This will be useful when you are publishing your project as npm package so that other developers can use this project based on it
scripts - This contains automated scripts which you can run using the name of the script specified. This actually contains npm commands to run via command line
repository - This is pointed the scm code location of the application
engines - This contains an array of entries which contains nodejs/npm version works for this application

"engines": {
  "node": ">= 8.0.0",
  "npm": ">= 3.1.0",
}
Browser list This will specify the list of supported browsers for your application

npm install - add a dependency in package.json 

Once the basic package.json file is created, you can install node package using npm install --save package name command. The below example explains about following things How to install npm modules/packages in nodejs applications How to add dependency and devdependencies to nodejs project The below command installs sweetalert and lodash module to nodejs project.
npm install --save sweetalert
npm install --save-dev lodash
The above commands create one entry in the dependencies section and other entry in devDependencies section of package.json. Here is updated package.json file

  "dependencies": {
    "sweetalert2": "^7.26.9"
 },
 "devDependencies": {
    "lodash": "^4.17.10"
 }

npm uninstall - remove the dependency in package.json 

You can uninstall dependencies from the application using npm uninstall command The below example explains about following things How to uninstall npm modules/packages in nodejs applications How to remove dependency and devDependencies from nodejs project The below command uninstalls sweetalert and lodash module to nodejs project.


npm uninstall --save sweetalert
npm uninstall --save-dev lodash
The above commands remove an entry from dependencies and devDependencies section of package.json

  "dependencies": {
 },
 "devDependencies": {
 }

How to update dependencies to latest versions automatically in a nodejs project.

package.json contains all dependencies. It is very painful to update each package to the latest version. There are many ways we can do that, npm-check-updates is the command line utility tool used to update all dependencies to latest version and will update the latest versions in package.json. Install npm-check-updates globally using npm install Once installed, ncu command will be available update all packages to the latest version using ncu -u command Finally install all packages in an application using npm install command
npm i -g npm-check-updates
ncu -u
npm install

Dependencies Types

There are a lot of dependencies types in package.json

  • Dependencies for production build 
  • devDependencies for a development build 
  • peerDependencies for production build minus transitive dependency optionalDependencies - optional or conditional dependencies

npm package versions

package.json dependencies contain package and version. The version can be configured or updated with below different options
Versions can be prefixed with a caret(^)  and tilde(~) and other symbols as below
For example 
^2.1.0 - This package can be updated to minor versions like 2.1.1 or 2.2.0
~2.1.0 - This package can be updated to minor versions like 2.1.1 but not 2.2.0
* means any version can be updated
>2.1.0 Any version which is greater than 2.1.0 can be updated
>=2.1.0 Any version which is greater than or equal 2.1.0 can be updated <2.1.0 Any version which is lesser than 2.1.0 can be updated
<=2.1.0 Any version which is lesser than equal 2.1.0 can be updated

What is the difference between Dependencies, DevDependencies, and peerDependencies in package.json

Dependencies are actual dependencies used to run your application. This will be used for Production. This will also install transitive dependencies, Transitive dependencies are if package 1 dependent on package 2 which dependent on package3

if package 1 is installed, package2 and package 3 also installed automatically.

This will be installed using npm install --save command. This always contains like Angular/Vue/react l, libraries as dependencies

DevDependencies 
these dependencies are development dependencies of an application.  This are needed as a dependency during development like testing frameworks -mocha or karma. 
This will not be published as part of production build. This will not install transitive dependency
This will be installed using npm install --save-dev command. This always contains like testing frameworks or documentation libraries

peerDependencies

These are dependencies like production but transitive dependencies are not installed. 
This will be installed using npm install --save command.

what is private key in package.json

package.json contains private key and value are true/false
if private is true, the application cannot be published as npm package. This is used to avoid publishing it to repository accidently

Related article


EmoticonEmoticon