npm
The full name is node package manager , which is the node package manager
To understand npm, we must first understand the most important file in npm, namely package.json
( package description file )
1.package.json
We recommend that each project should have a package.json
file, which can describe the basic information of our project, and we need to use it when we use npm to perform a series of operationspackage.json
How to generate package.json
files?
instruction:npm init
package.json
When we enter the above command in the console, the console will generate a file with corresponding content according to our needs
Input command: npm init, input information according to our needs
After the input, the system will automatically create package.json
a file, the content is the information we filled in
Explanation :
entry point
: Entry file , that is, when we run the project, the system will execute the entry file specified by the entry point
Note : If we do not specify the entry file, the system will default to the index.js file as the entry file of our project
2. npm command
-
npm init
: Generate package.json file- npm init -y: Skip the wizard and directly generate the default package.json file
-
npm install xxx
: Download a third-party dependency library named xxx, which can be abbreviated asnpm i xxx
# npm install jquery
npm install xxx --save
: Download a third-party dependency library named xxx and save the dependency information inpackage.json
the file, abbreviated as:-S
# npm install jquery --save
What does using the --save command do?
Using the –save command when downloading a file will store the file information in the package.json file while downloading the file, for example
We execute the following command
# npm install jquery --save
package.json
The dependency information of jquery has been added to the dependencies that can be found
With this dependency information, when we accidentally delete the dependent package or cause problems with the dependent package due to some operations, we can easily restore the dependent package
npm install
:package.json
Install all the dependencies saved in dependencies at one time
This confirms --save
the necessity of the above
For example, if I delete node_modules now, and then execute npm install
the command, I will find that npm directly downloads our original dependency package for us, that is to say:
package.json
As long as there is dependency information in our file , executing npm install
the command can help us re-download the dependency package corresponding to the dependency information
-
npm uninstall xxx
: Uninstall the dependency library named xxx, and the dependency information in packge.json is still savednpm uninstall xxx --save
: When uninstalling the dependent library, the dependency information is also deleted
-
npm -v
: View npm version number -
npm install --global npm
: Upgrade the npm version number to the latest version
3. Download the Taobao image
The npm server is abroad, and the domestic download speed is very slow. In order to solve this problem, Taobao made a mirror server cnpm for npm
Installation instructions:
# npm install --global cnpm
After the download is complete, you can use cnpm instead of npm, and the operation method is the same as npm
If you don't want to install cnpm and want to use Taobao's server to download
You can add this paragraph after each npm directive:--registry=http://registry.npm.taobao.org
# npm install xxx --registry=http://registry.npm.taobao.org
Add this option directly to the configuration file once and for all
# npm config srt registry http://registry.npm.taobao.org
After configuration, all your npm operations will be downloaded through Taobao's server by default