Monday 1 October 2018

npm: Global vs local installation of modules

‘npm’ provides two ways to install modules.
a.   Global Installation
b.   Local Installation

Global Installation
This install modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. It also installs man pages in {prefix}/share/man, if they’re supplied.

Local Installation
This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/, and man pages aren’t installed at all.

Global installation example
By default, node installs the packages in local mode. Use below command to install a module globally.

npm install <MODULE_NAME> -g

‘npm install express -g’ installs express module globally.

C:\>npm install express -g
+ express@4.16.3
added 50 packages from 47 contributors in 16.261s

In my windows system, express module installed under below directory.
C:\Users\Krishna\AppData\Roaming\npm\node_modules

Print all the globally installed modules
You can use the command ‘npm ls -g’ to print all the globally installed modules.

C:\>npm ls -g
C:\Users\Krishna\AppData\Roaming\npm
`-- express@4.16.3
  +-- accepts@1.3.5
  | +-- mime-types@2.1.19
  | | `-- mime-db@1.35.0
  | `-- negotiator@0.6.1
  +-- array-flatten@1.1.1
  +-- body-parser@1.18.2
  | +-- bytes@3.0.0
  | +-- content-type@1.0.4 deduped
  | +-- debug@2.6.9 deduped
  | +-- depd@1.1.2 deduped
  | +-- http-errors@1.6.3
  | | +-- depd@1.1.2 deduped
  | | +-- inherits@2.0.3
  | | +-- setprototypeof@1.1.0 deduped
  | | `-- statuses@1.4.0 deduped
  | +-- iconv-lite@0.4.19
  | +-- on-finished@2.3.0 deduped
  | +-- qs@6.5.1 deduped
  | +-- raw-body@2.3.2
  | | +-- bytes@3.0.0 deduped
  | | +-- http-errors@1.6.2
  | | | +-- depd@1.1.1
  | | | +-- inherits@2.0.3 deduped
  | | | +-- setprototypeof@1.0.3
  | | | `-- statuses@1.4.0 deduped
  | | +-- iconv-lite@0.4.19 deduped
  | | `-- unpipe@1.0.0 deduped
  | `-- type-is@1.6.16 deduped
  +-- content-disposition@0.5.2
  +-- content-type@1.0.4
  +-- cookie@0.3.1
  +-- cookie-signature@1.0.6
  +-- debug@2.6.9
  | `-- ms@2.0.0
  +-- depd@1.1.2
  +-- encodeurl@1.0.2
  +-- escape-html@1.0.3
  +-- etag@1.8.1
  +-- finalhandler@1.1.1
  | +-- debug@2.6.9 deduped
  | +-- encodeurl@1.0.2 deduped
  | +-- escape-html@1.0.3 deduped
  | +-- on-finished@2.3.0 deduped
  | +-- parseurl@1.3.2 deduped
  | +-- statuses@1.4.0 deduped
  | `-- unpipe@1.0.0
  +-- fresh@0.5.2
  +-- merge-descriptors@1.0.1
  +-- methods@1.1.2
  +-- on-finished@2.3.0
  | `-- ee-first@1.1.1
  +-- parseurl@1.3.2
  +-- path-to-regexp@0.1.7
  +-- proxy-addr@2.0.4
  | +-- forwarded@0.1.2
  | `-- ipaddr.js@1.8.0
  +-- qs@6.5.1
  +-- range-parser@1.2.0
  +-- safe-buffer@5.1.1
  +-- send@0.16.2
  | +-- debug@2.6.9 deduped
  | +-- depd@1.1.2 deduped
  | +-- destroy@1.0.4
  | +-- encodeurl@1.0.2 deduped
  | +-- escape-html@1.0.3 deduped
  | +-- etag@1.8.1 deduped
  | +-- fresh@0.5.2 deduped
  | +-- http-errors@1.6.3 deduped
  | +-- mime@1.4.1
  | +-- ms@2.0.0 deduped
  | +-- on-finished@2.3.0 deduped
  | +-- range-parser@1.2.0 deduped
  | `-- statuses@1.4.0 deduped
  +-- serve-static@1.13.2
  | +-- encodeurl@1.0.2 deduped
  | +-- escape-html@1.0.3 deduped
  | +-- parseurl@1.3.2 deduped
  | `-- send@0.16.2 deduped
  +-- setprototypeof@1.1.0
  +-- statuses@1.4.0
  +-- type-is@1.6.16
  | +-- media-typer@0.3.0
  | `-- mime-types@2.1.19 deduped
  +-- utils-merge@1.0.1
  `-- vary@1.1.2

Local Installation Example

C:\Users\Public>npm install express
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Public\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Public\package.json'
npm WARN Public No description
npm WARN Public No repository field.
npm WARN Public No README data
npm WARN Public No license field.

+ express@4.16.3
added 50 packages from 47 contributors and audited 119 packages in 8.086s
found 0 vulnerabilities


Local installation installs the module in current working directory.

package.json file
package.json file is located at the root directory of the module/application, it defines the properties of a package.

For example, package.json (It is located at node_modules/express/) file of express module looks like below.

package.json

{
  "_from": "express",
  "_id": "express@4.16.3",
  "_inBundle": false,
  "_integrity": "sha1-avilAjUNsyRuzEvs9rWjTSL37VM=",
  "_location": "/express",
  "_phantomChildren": {},
  "_requested": {
    "type": "tag",
    "registry": true,
    "raw": "express",
    "name": "express",
    "escapedName": "express",
    "rawSpec": "",
    "saveSpec": null,
    "fetchSpec": "latest"
  },
  "_requiredBy": [
    "#USER",
    "/"
  ],
  "_resolved": "https://registry.npmjs.org/express/-/express-4.16.3.tgz",
  "_shasum": "6af8a502350db3246ecc4becf6b5a34d22f7ed53",
  "_spec": "express",
  "_where": "C:\\Users\\Public",
  "author": {
    "name": "TJ Holowaychuk",
    "email": "tj@vision-media.ca"
  },
  "bugs": {
    "url": "https://github.com/expressjs/express/issues"
  },
  "bundleDependencies": false,
  "contributors": [
    {
      "name": "Aaron Heckmann",
      "email": "aaron.heckmann+github@gmail.com"
    },
    {
      "name": "Ciaran Jessup",
      "email": "ciaranj@gmail.com"
    },
    {
      "name": "Douglas Christopher Wilson",
      "email": "doug@somethingdoug.com"
    },
    {
      "name": "Guillermo Rauch",
      "email": "rauchg@gmail.com"
    },
    {
      "name": "Jonathan Ong",
      "email": "me@jongleberry.com"
    },
    {
      "name": "Roman Shtylman",
      "email": "shtylman+expressjs@gmail.com"
    },
    {
      "name": "Young Jae Sim",
      "email": "hanul@hanul.me"
    }
  ],
  "dependencies": {
    "accepts": "~1.3.5",
    "array-flatten": "1.1.1",
    "body-parser": "1.18.2",
    "content-disposition": "0.5.2",
    "content-type": "~1.0.4",
    "cookie": "0.3.1",
    "cookie-signature": "1.0.6",
    "debug": "2.6.9",
    "depd": "~1.1.2",
    "encodeurl": "~1.0.2",
    "escape-html": "~1.0.3",
    "etag": "~1.8.1",
    "finalhandler": "1.1.1",
    "fresh": "0.5.2",
    "merge-descriptors": "1.0.1",
    "methods": "~1.1.2",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.2",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~2.0.3",
    "qs": "6.5.1",
    "range-parser": "~1.2.0",
    "safe-buffer": "5.1.1",
    "send": "0.16.2",
    "serve-static": "1.13.2",
    "setprototypeof": "1.1.0",
    "statuses": "~1.4.0",
    "type-is": "~1.6.16",
    "utils-merge": "1.0.1",
    "vary": "~1.1.2"
  },
  "deprecated": false,
  "description": "Fast, unopinionated, minimalist web framework",
  "devDependencies": {
    "after": "0.8.2",
    "connect-redis": "~2.4.1",
    "cookie-parser": "~1.4.3",
    "cookie-session": "1.3.2",
    "ejs": "2.5.7",
    "eslint": "2.13.1",
    "express-session": "1.15.6",
    "hbs": "4.0.1",
    "istanbul": "0.4.5",
    "marked": "0.3.17",
    "method-override": "2.3.10",
    "mocha": "3.5.3",
    "morgan": "1.9.0",
    "multiparty": "4.1.3",
    "pbkdf2-password": "1.2.1",
    "should": "13.2.1",
    "supertest": "1.2.0",
    "vhost": "~3.0.2"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "homepage": "http://expressjs.com/",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "license": "MIT",
  "name": "express",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/expressjs/express.git"
  },
  "scripts": {
    "lint": "eslint .",
    "test": "mocha --require test/support/env --reporter spec --bail --check-leaks --no-exit test/ test/acceptance/",
    "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks --no-exit test/ test/acceptance/",
    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks --no-exit test/ test/acceptance/",
    "test-tap": "mocha --require test/support/env --reporter tap --check-leaks --no-exit test/ test/acceptance/"
  },
  "version": "4.16.3"
}

Uninstall a module
'npm uninstall <MODULE>' is used to uninstall a module

C:\Users\Public>npm uninstall express
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Public\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Public\package.json'
npm WARN Public No description
npm WARN Public No repository field.
npm WARN Public No README data
npm WARN Public No license field.

removed 1 package and audited 147 packages in 3.394s
found 0 vulnerabilities

Update a module
'npm update <MODULE>' updates the module.

For example 'npm update express' updates express module.

That’s it..In my next post, I am going to explain how to create custom module and publish it.

Note
a.   If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
b.   If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries are placed in your PATH environment variable.

c.   You can add all the dependencies in package.json file and execute ‘npm install’ command to download the dependencies.


Previous                                                 Next                                                 Home

No comments:

Post a Comment