Understanding and use of npm package manager in node.js

npmThe 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.jsonfile, 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.jsonfiles?

instruction:npm init

package.jsonWhen 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

insert image description here

After the input, the system will automatically create package.jsona 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 in package.jsonthe 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.jsonThe dependency information of jquery has been added to the dependencies that can be found
insert image description here

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.jsonInstall all the dependencies saved in dependencies at one time

This confirms --savethe necessity of the above

For example, if I delete node_modules now, and then execute npm installthe command, I will find that npm directly downloads our original dependency package for us, that is to say:

package.jsonAs long as there is dependency information in our file , executing npm installthe command can help us re-download the dependency package corresponding to the dependency information

insert image description here

  • npm uninstall xxx: Uninstall the dependency library named xxx, and the dependency information in packge.json is still saved

    • npm 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

Guess you like

Origin blog.csdn.net/Laollaoaolao/article/details/121543416