Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

NPM Basic Tutorial

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