In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use NPM". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use NPM.
NPM is a package management tool installed with NodeJS, which can solve many problems in NodeJS code deployment. Common usage scenarios are as follows:
Allows users to download third-party packages written by others from the NPM server for local use.
Allows users to download and install command-line programs written by others from the NPM server for local use.
Allows users to upload their own packages or command-line programs to the NPM server for others to use.
Since the new version of nodejs has been integrated with npm, npm has been installed before. You can also test whether the installation is successful by typing "npm-v". The command is as follows, and a version prompt indicates that the installation is successful:
$npm-v2.3.0
If you are installing an older version of npm, you can easily upgrade it with the npm command, as follows:
$sudo npm install npm- g/usr/local/bin/npm-> / usr/local/lib/node_modules/npm/bin/npm-cli.jsnpm@2.14.2 / usr/local/lib/node_modules/npm
If it is a Window system, use the following command:
Npm install npm-g
Use the command of Taobao image:
Npm install-g cnpm-- registry= https://registry.npm.taobao.org
Use the npm command to install the module
The syntax format of the npm installation Node.js module is as follows:
$npm install
For the following example, we use the npm command to install the commonly used Node.js web framework module express:
$npm install express
After installation, the express package is placed in the node_modules directory under the project directory, so you only need to require ('express') in the code without specifying the third-party package path.
Var express = require ('express')
Global installation versus local installation
The package installation of npm is divided into local installation (local) and global installation (global). From the point of view of the command line, the only difference is whether there is-g, such as
Npm install express # Local installation npm install express-g # Global installation
If the following error occurs:
Npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
The solution is:
$npm config set proxy null
Local installation
1. Place the installation package under. / node_modules (the directory where the npm command was run), and if there is no node_modules directory, the node_modules directory will be generated in the directory where the npm command is currently executed.
two。 Locally installed packages can be introduced through require ().
Global installation
1. Put the installation package under / usr/local or your node installation directory.
two。 You can use it directly on the command line.
If you want to have both, you need to install it in two places or use npm link.
Next, we install express globally.
$npm install express-g
The installation process outputs the following, and the first line outputs the version number and installation location of the module.
Express@4.13.3 node_modules/express ├── escape-html@1.0.2 ├── range-parser@1.0.2 ├── merge-descriptors@1.0.0 ├── array-flatten@1.1.1 ├── cookie@0.1.3 ├── utils-merge@1.0.0 ├── parseurl@1.3.0 ├── cookie-signature@1.0.6 ├── methods@1.1.1 ├── fresh@0 .3.0 ├── vary@1.0.1 ├── path-to-regexp@0.1.7 ├── content-type@1.0.1 ├── etag@1.7.0 ├── serve-static@1.10.0 ├── content-disposition@0.5.0 ├── depd@1.0.1 ├── qs@4.0.0 ├── finalhandler@0.4.0 (unpipe@1.0.0) ├── on-finished@ 2.3.0 (ee-first@1.1.1) ├── proxy-addr@1.0.8 (forwarded@0.1.0 Ipaddr.js@1.0.1) ├── debug@2.2.0 (ms@0.7.1) ├── type-is@1.6.8 (media-typer@0.3.0, mime-types@2.1.6) ├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.6) └── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1 Mime@1.3.4, http-errors@1.3.1)
View installation information
You can view all globally installed modules using the following command:
$npm list-g ├─┬ cnpm@4.3.2 │ ├── auto-correct@1.0.0 │ ├── bagpipe@0.3.5 │ ├── colors@1.1.2 ├─┬ ├─┬ │ │ └── graceful-readlink@1.0.1 │ ├─┬ cross-spawn@0.2.9 │ │ └── lru-cache@2.7.3.
If you want to see the version number of a module, you can use the following command:
$npm list gruntprojectName@projectVersion / path/to/project/folder └── grunt@0.4.1
Use package.json
Package.json is located in the module's directory and is used to define the properties of the package. Next, let's take a look at the package.json file of the express package, which is located in the node_modules/express/package.json content:
{"name": "express", "description": "Fast, unopinionated, minimalist web framework", "version": "4.13.3", "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": "git+ https://github.com/strongloop/express.git"}," homepage ":" http://expressjs.com/", "keywords": ["express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api"], "dependencies": {"accepts": "~ 1.2.12" "array-flatten": "1.1.1", "content-disposition": "0.5.0", "content-type": "~ 1.0.1", "cookie": "0.1.3", "cookie-signature": "1.0.6", "debug": "~ 2.2.0", "depd": "~ 1.0.1", "escape-html": "1.0.2" "etag": "~ 1.7.0", "finalhandler": "0.4.0", "fresh": "0.3.0", "merge-descriptors": "1.0.0", "methods": "~ 1.1.1", "on-finished": "~ 2.3.0", "parseurl": "~ 1.3.0", "path-to-regexp": "0.1.7" "proxy-addr": "~ 1.0.8", "qs": "4.0.0", "range-parser": "~ 1.0.2", "send": "0.13.0", "serve-static": "~ 1.10.0", "type-is": "~ 1.6.6", "utils-merge": "1.0.0", "vary": "~ 1.0.1"} "devDependencies": {"after": "0.8.1", "ejs": "2.3.3", "istanbul": "0.3.17", "marked": "0.3.5", "mocha": "2.2.5", "should": "7.0.2", "supertest": "1.0.1", "body-parser": "~ 1.13.3" "connect-redis": "~ 2.4.1", "cookie-parser": "~ 1.3.5", "cookie-session": "~ 1.2.0", "express-session": "~ 1.11.3", "jade": "~ 1.11.0", "method-override": "~ 2.3.5", "morgan": "~ 1.6.1", "multiparty": "~ 4.1.2" "vhost": "~ 3.0.1"}, "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-ci": "istanbul cover node_modules/mocha/bin/_mocha-report lcovonly-require test/support/env-reporter spec-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/"}, "gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e", "bugs": {"url": "https://github.com/strongloop/express/issues"}," _ id ":" express@4.13.3 "," _ shasum ":" ddb2f1fb4502bf33598d2b032b037960ca6c80a3 "," _ 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": "dougwilson", "email": "doug@somethingdoug.com"}, {"name": "rfeng", "email": "enjoyjava@gmail.com"}, {"name": "aredridel", "email": "aredridel@dinhe.net"}, {"name": "strongloop" "email": "callback@strongloop.com"}, {"name": "defunctzombie", "email": "shtylman@gmail.com"}], "dist": {"shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3", "tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz"}," directories ": {} "_ resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz"," readme ":" ERROR: No README data found! "}
Package.json attribute description
Name-package name.
Version-the version number of the package.
Description-description of the package.
Homepage-url, the official website of the package.
Author-the name of the author of the package.
Contributors-the name of the other contributor to the package.
Dependencies-list of dependent packages. If the dependency package is not installed, npm automatically installs the dependency package in the node_module directory.
Repository-the type of place where the package code is stored, which can be git or svn,git on Github.
The main-main field specifies the program's main entry file, which is loaded by require ('moduleName'). The default value for this field is index.js under the module root.
Keywords-keyword
Uninstall module
We can uninstall the Node.js module using the following command.
$npm uninstall express
After uninstalling, you can check whether the package still exists in the / node_modules/ directory, or use the following command to check:
$npm ls
Update module
We can update the module with the following command:
$npm update express
Search module
Use the following to search for modules:
$npm search express
Create a module
To create modules, package.json files are essential. We can use NPM to generate a package.json file that contains the basic results.
$npm initThis utility will walk you through creating a package.json file.It only covers the most common items And tries to guess sensible defaults.See `npm help json`for definitive documentation on these fieldsand exactly what they do.Use `npm install-- save` afterwards to install a package andsave it as a dependency in the package.json file.Press ^ C at any time to quit.name: (node_modules) runoob # module name version: (1.0.0) description: Node.js test module (www.runoob.com) # description entry point: (index.js) test command: Make testgit repository: https://github.com/runoob/runoob.git # Github address keywords: author: license: (ISC) About to write to. / node_modules/package.json: # generate address {"name": "runoob", "version": "1.0.0", "description": "Node.js Test Module (www.runoob.com)", …... } Is this ok? (yes) yes
You need to enter the above information according to your own situation. After entering "yes" at the end, the package.json file is generated.
Next, we can use the following command to register users in the npm repository (using mailbox registration):
$npm adduserUsername: mcmohdPassword:Email: (this IS public) mcmohd@gmail.com
Next we release the module with the following command:
$npm publish
If you have done all the above steps correctly, you can use npm to install it like other modules.
Version number
You are exposed to the version number when downloading and publishing code using NPM. NPM uses semantic version numbers to manage code, which is briefly introduced here.
The semantic version number is divided into X.Y.Z three digits, which represent the major version number, the minor version number and the patch version number respectively. When the code changes, the version number is updated according to the following principles.
If you are just fixing bug, you need to update the Z bit.
If it is a new feature, but it is backward compatible, you need to update the Y bit.
If there is a big change, the downwards are not compatible, and the X bit needs to be updated.
With this guarantee, the version number can rely on a range of version numbers in addition to a fixed version number when declaring third-party package dependencies. For example, "argv": "0.0.x" indicates the latest version of argv that depends on the 0.0.x series.
All the version number ranges supported by NPM can be specified to view official documents.
NPM common commands
In addition to the sections introduced in this chapter, NPM provides a lot of functionality, and there are many other useful fields in package.json.
In addition to being able to view the official documentation at [https://npmjs.org/doc/], here are some common NPM commands.
NPM provides a number of commands, such as install and publish, all of which can be viewed using npm help.
NPM provides a number of commands, such as install and publish, all of which can be viewed using npm help.
Use npm help to view detailed help for a command, such as npm help install.
Use npm install. Package.json in the same directory as npm install. -g can first install the current command line program locally, which can be used for local testing before release.
Using npm update, you can update the corresponding modules in the node_ modules subdirectory under the current directory to the latest version.
Use npm update-g to update the globally installed corresponding command line program to the latest version.
Use npm cache clear to clear the NPM local cache against people who release new versions of code with the same version number.
Use npm unpublish @ to unpublish a version of your code that you have released.
Use Taobao NPM image
We all know that it is very slow to directly use the official image of npm in China. Taobao NPM image is recommended here.
Taobao NPM image is a full npmjs.org image, which you can use instead of the official version (read-only). The synchronization frequency is currently 10 minutes to ensure that it is synchronized with the official service as much as possible.
You can use Taobao's customized cnpm (gzip Compression support) command line tool instead of the default npm:
$npm install-g cnpm-- registry= https://registry.npm.taobao.org
This allows you to install the module using the cnpm command:
$cnpm install [name] Thank you for reading, the above is the content of "how to use NPM". After the study of this article, I believe you have a deeper understanding of how to use NPM, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.