NPM-Node Package Manager-Node Package Manager
1. What is NPM?
- npm (full name Node Package
Manager, namely node package manager) is Node.js default package management system written in JavaScript. - npm official website
- npm official documentation
Generally, when we say npm, we may refer to 3 things
- NPM website: https://www.npmjs.com/
- NPM package management library, which stores a large number of JavaScript code libraries
- NPM client, the npm command line tool we use. The node.js-based command line tool developed using JavaScript is also a package of Node itself.
NPM official explanation
- npm is a JavaScript package manager and the largest software registry in the world.
- A new and powerful way to discover reusable code and integrate code packages into projects.
- npm makes it easier for JavaScript developers to share and reuse code, and it also makes it easier for us to update the code that is being shared.
Two, npm and nonde.js
- npm is the default package management system for node.js. After node is installed, npm will be installed by default
- npm itself is also a package (software) developed based on node.js
3. How to install npm?
- npm will be automatically installed with nonde.js, and npm will be automatically installed after node.js is installed
- View the current npm version:
npm -v
- Update npm:
npm install npm@latest -g
Fourth, the use of NPM
- Find the required package at https://www.npmjs.com/ l website
- In the root directory of the project, perform the
npm install
package name installation require('包名');
Load the module in the node.js code- Note: The
npm install 包名
installation package will automatically download to the directory of the currentnode_modules
directory, if the directory does not exist, it is created, if it exists downloaded directly into it. - By
require('包名');
loading the module in the code-
this method mentioned above is called local installation-
Introduction to NPM global installation
- What is npm global installation?
npm install包名 -g
Npm global installation refers to installing the package as a command line tool.
//通过npm全局安装mime
npm install mime -g
//安装完毕后可以在命令行中直接使用
mime a.txt命令来查看对应的结果
- The npm global installation actually does two things:
1. Download the package to a specified directoryC:\Users\username\AppData\lRoaming \npm\node_modules
2. Create a section of code to be executed by the command line.c: \Userslusername\AppData\Roaming \npm\mime -> C:\Users\stevexiaohu zhao\AppData\Roaming \npminode_modules imime\cli.js
NPM installation recommendations
The global installation is just to be used as a command line
Five, npm common command introduction
- install, install the package.
npm install 包名
- uninstall, uninstall the package.
npm uninstall 包名
- version, view the current npm version.
npm version或npm -v
- init, create a package.json file.
npm init
- Note: When using
npm init -y
, if the name of the current folder (directory) is strange (with uppercase, with Chinese, etc.), it will affectnpm init -y
the one-step generation operation. At this time, it needsnpm init
to be generated according to the wizard.
The difference between "Modules" and "Packages"
- A module is any file or directory that can be loaded by Node.js’
require()
- The module can be any file or directory (there can be many files in the directory), as long as it can be passed require() by node.js.
- A package is a file or directory that is described by a
package. json
. This can happen in a bunch ofdifferent ways!
- A package is a file or directory (there can be multiple files in the directory), and it must be described by a package.json file, and it can be a package.