What is npm and how it works

npm is the world's largest Software Library (Registry), and it is also a software Package Manager and Installer for the javascript projects. npm is widely used by nodejs, angular, react, vuejs frameworks, you can create your own package and publish the package to the marketplace. it is very helpful to share the code, plugin to with other developers and open source community. npm has boosted the open source community to the new level.

How to install

To use the npm, you need to install the nodejs, npm is in-built with nodejs. You can download the nodejs from the official site nodejs.org, After downloading, please install it.

  • For windows Install it in the c drive, and copy the installed location path C:\Program Files\nodejs and add it in your enviorement variables

  • Right click on my computer and then properties

  • Now click on the Advanced system settings (Left bar)

  • Now click on the path and add the new path

How npm package works

After installing the nodejs, you can verify the installation by running the npm --version command from your command runner.

What is package.json

package.json file is used to maintain the all npm package with the version. this file contains all the npm packages you installed.

How to generate the package.json file

npm provides npm init command to generate the package.json file while running this command it will ask some details like - package name: Name of the package you want to add like demo-app

  • version: version by default 1.0.0
  • description: add description
  • entry point: when you run npm start, the provided entry point will run, you can add index.js or leave blank it
  • test command: Any script you want to use for testing
  • git repo path: You can provide your GitHub repo URL
  • keywords: For searching and SEO purpose
  • author: Add the name of the author
  • licence: Add like MIT or ISC

How to install the npm package

You can get the npm package you want to install from the NpmJS You can install the package globally and locally

  • a. Globally

    To install any package you need to add -g prefix like npm i expressjs -g : it will install the expressjs globally, you don't need to install it for your projects individually. The package will be accessibly globally.

  • b. Locally

    npm i expressjs : it will install the expressjs locally, the package will be only accessible to your local project.

List of npm common commands

Below is the list of all npm commands

  • npm init: to generate the package.json file
  • npm i or npm install: to install all packages of the package.json file
  • npm i -g: install the package globally
  • npm start: It will execute the entry point script you added in the package.json file
  • npm install npm@latest -g: Upgrade the npm package to the latest version
  • npm outdated: list of all out date packages
  • npm list -g: list of all global npm packages
  • npm uninstall "package name": it will uninstall the package
  • npm publish: This is used to publish the package at npmjs

Related blog of nodejs