NPM Basic Tutorial
Module vs Package
- The module may have a group of functions and which is stored in a file to import and reuse
- The package is a directory that contains one or more modules
- A package contains a package.json file which contains the metadata about the package
NPM Help:
- Run the following command in the terminal
- npm help: It gives the list of npm commands and minor information about the same
- npm install -h: It gives more information about the single command
- npm help install: It gives the complete information about the command
// get help npm help // command detail // npm help install
Create package.json
- The package.json file holds the metadata of your project
- Metadata is a track of dependency of a project
- Running the command npm init command creates a package.json file, followed by few questions”
Install packages
- Installing npm packages is very easy
- Command: npm install package-name
- Example: npm install express
- You can also install dev dependency by running the below command
- Command: npm install nodemon –save-dev
- You can unistall the package by running the below command
- Command: npm unistall express
// Install package npm install express // Install dev dependency npm install nodemon --save-dev // Unistall package npm unistall express
Npm scripts
- You can run simple scripts by defining in the package.json
- The scripts property in the package.json file holds the list of scripts
"scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" },
Semantic versioning
- You can specify which update types your package can accept from dependencies in your package’s package.json file
- For example, to specify acceptable version ranges up to 1.0.4, use the following syntax
- Patch releases: 1.0 or 1.0.x or ~1.0.4
- Minor releases: 1 or 1.x or ^1.0.4
- Major releases: * or x
// Example "dependencies": { "express": "^3.0.0", "hapi": "~3.2.0" }