NPM 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
You should read it
May be interested
- Things you didn't know about 26 types of Pokéball - Part 1pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
- 10 tips to help you become a great Pokemon trainerin the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
- How to use GoChat application in Pokémon GOpokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
- How to play Pokemon GO in Landscape Mode on the iPhonealthough players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
- The secret to controlling Pokemon Go employees at workthese days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
- Check out the 'buffalo' Pokémon in Pokémon Goeach type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
- Sitting home can also locate Pokemon around, do you believe it?the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
- 5 undeniable benefits when playing Pokemon Goget to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
- Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
- Pokémon systems when fighting in Pokémon Goeach pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.