What is the NPM
What is NPM? NPM is the Package Manager for Node.js. Why do we need a package management tool? Because when we develop on Node.js, we use a lot of JavaScript code written by other people. If we want to use a package written by someone else, every time we search the official website according to the name, download the code, decompress, script introduction, very tedious. So a centralized management tool came into being: everyone packaged their own modules and put them on the NPM website. If they want to use them, they can use them directly through the NPM installation, regardless of where the code is stored or downloaded from. More importantly, if we want to use module A, and module A depends on module B, and module B depends on modules X and Y, NPM can download and manage all dependent packages based on their dependencies. Otherwise, rely on our own manual management, both troublesome and easy to make mistakes.
The history of NPM
In the days before NPM, how did you go about getting a module, or a framework?
- Access – > JQ
- Access – > Boottrap
- Access – > the Underscore
Of course, it was also a time when jQuery was dominating
We programmers, of course, can’t accept this archaic and inefficient thing. We need more efficient and rational code management. Think of other languages
language | Package management tool |
---|---|
Java | maven |
Python | Distribute, SetupTools, Distutils, easy_install, and PIP |
PHP | Composer |
No front end! Intolerable! And at this point, a man came forward, Isaac Z. Schlueter GitHub for short.
General implementation ideas
- Find a cloud service that manages all your code
- Then tell the authors of jQuery, Bootstrap, and so on to publish to the cloud using NPM Publish,
- The rest of the community can download the code through NPM Install if they want to use it.
- The downloaded code appears in the node_modules directory and can be used via require import.
The follow-up development
Isaaz informs jQuery author John Resig, will he say yes? Not necessarily unknown, but do it anyway. Only when front-end developers know it’s there will they agree it’s right. So how did NPM catch on so quickly in the front-end community? The growth of NPM goes hand in hand with the growth of Node.js. Node.js was written by Ryan Dahl github, an American programmer working in Germany. He wrote Node.js, but Node.js lacked a package manager at the time, so he and Isaaz hit it off, and eventually Node.js had NPM built in. As you all know, Node.js took off.
package.json
This file is created when we initialize it in an empty file called NPM init,
Here I looked for package.json for Express
{
"name": "express", / / package name"description": "Fast, unopinionated, minimalist web framework"// Package description"version": "4.13.3"// Package version number"author": {// The author name of the package"name": "TJ Holowaychuk"."email": "[email protected]"
},
"contributors": [// Names of other contributors to the package {"name": "Aaron Heckmann"."email": "[email protected]"} / /... ] ."license": "MIT"// You should have a protocol for your module that lets users know what permissions they have to use your module and what restrictions they have on using it."repository"Git: {// The type of place where the package code is stored, either git or SVN, git is available on Github."type": "git"."url": "git+https://github.com/strongloop/express.git"
},
"homepage": "http://expressjs.com/"// The package's official url"keywords": [// keyword"express"."framework"."sinatra"."web"."rest"."restful"."router"."app"."api"]."dependencies": {// List of dependent packages. If the dependency package is not installed, NPM automatically installs the dependency package in the node_module directory"accepts": "~ 1.2.12",
// ...
},
"devDependencies": {// Dependencies for the development environment"after": "0.8.1",
// ...
},
"engines": {// Node version range"node": "> = 0.10.0"
},
"files": [
"LICENSE"."History.md"."Readme.md"."index.js"."lib/"]."scripts": {// Commands that need to be executed at each stage of the project lifecycle. Key is the life cycle event, and value is the command to execute."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": "[email protected]"."_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3"."_from": "express@*"."_npmVersion": "1.4.28"."_npmUser": {
"name": "dougwilson"."email": "[email protected]"
},
"maintainers": [{"name": "tjholowaychuk"."email": "[email protected]"} / /... ] ."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!"
}
Copy the code
So next time you have package.json you can just download and install these dependencies. The package file will appear in node_modules.
Frequently used commands
NPM install -g and NPM install -g are different from NPM install -g and NPM install -g is different from NPM install -g
npm install express # Local install
npm install express -g # global install
Copy the code
Local installation 1. Place the installation package in./node_modules (the directory where NPM is run). Node_modules will be generated in the directory where the NPM command is currently executed. Install the installation package in /usr/local or in your Node installation directory
If you can install a module, there’s a way to uninstall a module,
npm uninstall express
Copy the code
After uninstalling, check to see if the module exists
npm ls
Copy the code
The update module
npm update express
Copy the code
More knowledge we’ll be at NPM will you really? (2) to explain you please look forward to.