NPM in Node.js

Provides utilities to install Node.js packages, version management and dependency management of packages in Node.js.

NPM stands for Node Package Manager which provides the following two functions:

Create online repository for node.js that can be found at search.nodejs.org

Provides utilities to install Node.js packages, version management and dependency management of packages in Node.js.

NPM is bundled with Node.js from version v0.6.3. To confirm this, open the command line window, type the following commands and see the results:

 $ npm --version 
2.7.1

If you run npm older systems, you can easily update to the latest version. Use the following command as root:

 $ sudo npm install npm -g 
/ usr / bin / npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.7.1 / usr / lib / node_modules / npm

Install the Module using npm in Node.js

Here is a simple syntax to install any Node.js Module:

 $ npm install 

For example: Below is the command to install a very popular module in Node.js, which is the Express module:

 $ npm install express 

Now to use this module in your js file, use the following syntax:

 var express = require ( 'express' ); 

Global installation and local installation

By default, npm installs any dependencies on the local computer. Here, this module targets installation packages in the node_modules directory in the Node application directory. This local package can be deployed based on the require () method. For example, when you install the Express Module, create the node_modules directory in the current directory where you can install this Module.

 $ ls - l total 0 drwxr - xr - x 3 root root 20 Mar 17 02 : 23 node_modules 

Alternatively, you can use the npm ls command to list the Module installation directories.

The global installation package is stored in the system files. Dependencies can be used with the CLI (Command Line Interface) functions of node.js but cannot be imported using the require () use of the Node application directly.

Now try installing the Express Module using the following global settings:

 $ npm install express -g 

This will give the same result but the module is installed globally. Here, the first line tells us about the Module version and where the drivers are located at the start.

 express@4.12.2 /usr/lib/node_modules/express ├── merge-descriptors@1.0.0 ├── utils-merge@1.0.0 ├── cookie-signature@1.0.6 ├── methods@1.1.1 ├── fresh@0.2.4 ├── cookie@0.1.2 ├── escape-html@1.0.1 ├── range-parser@1.0.2 ├── content-type@1.0.1 ├── finalhandler@0.3.3 ├── vary@1.0.0 ├── parseurl@1.3.0 ├── content-disposition@0.5.0 ├── path-to-regexp@0.1.3 ├── depd@1.0.0 ├── qs@2.3.3 ├── on-finished@2.2.0 (ee-first@1.1.0) ├── etag@1.5.1 (crc@3.2.1) ├── debug@2.1.3 (ms@0.7.0) ├── proxy-addr@1.0.7 (forwarded@0.1.0, ipaddr.js@0.1.9) ├── send@0.12.1 (destroy@1.0.3, ms@0.7.0, mime@1.3.4) ├── serve-static@1.9.2 (send@0.12.2) ├── accepts@1.2.5 (negotiator@0.5.1, mime-types@2.0.10) └── type-is@1.6.1 (media-typer@0.3.0, mime-types@2.0.10)

You can also use the following command to check global installation versions:

 $ npm ls -g 

Use package.json package

Package.json is displayed in the root directory of any Node application and is used to define package properties. Together opening a package.json file of express package displayed in node_modules / express /:

 { "name" : "express" , "description" : "Fast, unopinionated, minimalist web framework" , "version" : "4.11.2" , "author" : { "name" : "TJ Holowaychuk" , "email" : "tj@vision-media.ca" }, "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" } ], "license" : "MIT" , "repository" : { "type" : "git" , "url" : "https://github.com/strongloop/express" }, "homepage" : "http://expressjs.com/" , "keywords" : [ "express" , "framework" , "sinatra" , "web" , "rest" , "restful" , "router" , "app" , "api" ], "dependencies" : { "accepts" : "~1.2.3" , "content-disposition" : "0.5.0" , "cookie-signature" : "1.0.5" , "debug" : "~2.1.1" , "depd" : "~1.0.0" , "escape-html" : "1.0.1" , "etag" : "~1.5.1" , "finalhandler" : "0.3.3" , "fresh" : "0.2.4" , "media-typer" : "0.3.0" , "methods" : "~1.1.1" , "on-finished" : "~2.2.0" , "parseurl" : "~1.3.0" , "path-to-regexp" : "0.1.3" , "proxy-addr" : "~1.0.6" , "qs" : "2.3.3" , "range-parser" : "~1.0.2" , "send" : "0.11.1" , "serve-static" : "~1.8.1" , "type-is" : "~1.5.6" , "vary" : "~1.0.0" , "cookie" : "0.1.2" , "merge-descriptors" : "0.0.2" , "utils-merge" : "1.0.0" }, "devDependencies" : { "after" : "0.8.1" , "ejs" : "2.1.4" , "istanbul" : "0.3.5" , "marked" : "0.3.3" , "mocha" : "~2.1.0" , "should" : "~4.6.2" , "supertest" : "~0.15.0" , "hjs" : "~0.0.6" , "body-parser" : "~1.11.0" , "connect-redis" : "~2.2.0" , "cookie-parser" : "~1.3.3" , "express-session" : "~1.10.2" , "jade" : "~1.9.1" , "method-override" : "~2.3.1" , "morgan" : "~1.5.1" , "multiparty" : "~4.1.1" , "vhost" : "~3.0.0" }, "engines" : { "node" : ">= 0.10.0" }, "files" : [ "LICENSE" , "History.md" , "Readme.md" , "index.js" , "lib/" ], "scripts" : { "test" : "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/" , "test-cov" : "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/" , "test-tap" : "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/" , "test-travis" : "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/" }, "gitHead" : "63ab25579bda70b4927a179b580a9c580b6c7ada" , "bugs" : { "url" : "https://github.com/strongloop/express/issues" }, "_id" : "express@4.11.2" , "_shasum" : "8df3d5a9ac848585f00a0777601823faecd3b148" , "_from" : "express@*" , "_npmVersion" : "1.4.28" , "_npmUser" : { "name" : "dougwilson" , "email" : "doug@somethingdoug.com" }, "maintainers" : [ { "name" : "tjholowaychuk" , "email" : "tj@vision-media.ca" }, { "name" : "jongleberry" , "email" : "jonathanrichardong@gmail.com" }, { "name" : "shtylman" , "email" : "shtylman@gmail.com" }, { "name" : "dougwilson" , "email" : "doug@somethingdoug.com" }, { "name" : "aredridel" , "email" : "aredridel@nbtsc.org" }, { "name" : "strongloop" , "email" : "callback@strongloop.com" }, { "name" : "rfeng" , "email" : "enjoyjava@gmail.com" } ], "dist" : { "shasum" : "8df3d5a9ac848585f00a0777601823faecd3b148" , "tarball" : "http://registry.npmjs.org/express/-/express-4.11.2.tgz" }, "directories" : {}, "_resolved" : "https://registry.npmjs.org/express/-/express-4.11.2.tgz" , "readme" : "ERROR: No README data found!" } 

Package.json properties

name - the name of the package

version - package version

description - description of package

homepage - package's homepage

author - the author of the package

contributors - contributor name for package

dependencies - List of dependencies, automatically installed.

repository - package type and url of the package

main - the entry point of the package

keywords - keywords

Uninstall a module in Node.js

Use the following lines to remove a Module in Node.js

 $ npm uninstall express 

When uninstalling this package, you can confirm the contents in the / node_modules / directory using the following command:

 $ npm ls 

Update a Module in Node.js

Update package.json package and change the version of the update that can be updated using the following command:

 $ npm update express 

Search for a Module in Node.js

Using npm, you can search for a Module as follows:

 $ npm search express 

Create a Module in Node.js

The module creation process requires the package.json package to be created.

 $ npm init 
Utilities này sẽ đến bạn qua tạo một tập tin gói.json.
It only covers the most common items, and tries to guess sane defaults.

See 'npm help json' for definitive documentation on these fields
and exactly what they do.

Dùng 'npm cài đặt --save' afterwards to cài đặt một gói và
save it as a dependency in the package.json file.

Press ^ C tại nào thời gian để thoát.
name: (webmaster)

You need to provide all the required information in using the Module. You can use the steps above in the package.json file to understand the meaning of the required information. When the package.json package is created, use the following commands to register with npm site using a valid email address.

 $ npm adduser 
Username: mcmohd
Password:
Email: (this IS public) mcmohd@gmail.com

Now publish your Module:

 $ npm publish 

If all goes well, it will be published and this folder will be accessed by others using the Node.js Module

According to Tutorialspoint

Last post: REPL Terminal in Node.js

Next article: Concept of Callbacks in Node.js

4 ★ | 2 Vote