Show List

Node Package Manager

Node package manager (NPM) comes bundled with Node.js and provides two main functionalities:
  • Provides access to online repository of Node.js packages and modules
  • Used to install Node.js packages, manage versions and dependencies.
To verify that npm is already installed, you can run "npm --version" command from command line:
C:\Users\mail2>npm --version
7.24.0

Installing a package

Command to install a package is "npm install package_name". 

Package can be installed in local mode or global mode. 

  • Local mode means that package will be installed in the node_modules directory of the current node application. Local package module can be accessed using the require() function. Command to install package is "npm install package_name". By default the packages are installed local.
  • Globally installed packages are stored in system directory. Such packages can be used in the CLI functions but can not be imported in node application directly using the require() function. Command to install global package "npm install package_name -g"
When a package in installed, package.json file is updated and installed package modules and dependent modules get added in the node_modules folder. 

For example, when upper-case package is installed using "npm i upper-case" command
  • package.json file gets updated with upper-case dependency.
  • upper-case module depends on tslib so both these modules get added under node_modules.
package.json
{
  "dependencies": {
    "upper-case": "^2.0.2"
  }
}

package-lock.json
{
  "name": "node-js-npm",
  "lockfileVersion": 2,
  "requires": true,
  "packages": {
    "": {
      "dependencies": {
        "upper-case": "^2.0.2"
      }
    },
    "node_modules/tslib": {
      "version": "2.4.0",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
      "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ=="
    },
    "node_modules/upper-case": {
      "version": "2.0.2",
      "resolved": "https://registry.npmjs.org/upper-case/-/upper-case-2.0.2.tgz",
      "integrity": "sha512-KgdgDGJt2TpuwBUIjgG6lzw2GWFRCW9Qkfkiv0DxqHHLYJHmtmdUIKcZd8rHgFSjopVTlw6ggzCm1b8MFQwikg==",
      "dependencies": {
        "tslib": "^2.0.3"
      }
    }
  },
  "dependencies": {
    "tslib": {
      "version": "2.4.0",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
      "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ=="
    },
    "upper-case": {
      "version": "2.0.2",
      "resolved": "https://registry.npmjs.org/upper-case/-/upper-case-2.0.2.tgz",
      "integrity": "sha512-KgdgDGJt2TpuwBUIjgG6lzw2GWFRCW9Qkfkiv0DxqHHLYJHmtmdUIKcZd8rHgFSjopVTlw6ggzCm1b8MFQwikg==",
      "requires": {
        "tslib": "^2.0.3"
      }
    }
  }
}

Source Code:
https://github.com/it-code-lab/node-js-npm

    Leave a Comment


  • captcha text