node detailed npm

Understanding npm

  • Third-party modules are some files written by others. If we want to use these files, we need to download them.
  • Node provides a tool called npm, dedicated to managing these modules. Can be downloaded, can be updated. . . In addition to managing third-party modules, npm can also manage some frameworks, plug-ins, and libraries.
  • So npm is called a package manager, and we collectively refer to modules, plugins, and libraries as packages.
  • The npm tool comes with npm when installing node.

Use npm

  • Testing tools: the command line input npm -vcan have a version number, it means that the tool can be used
    Insert picture description here

Download package

  • Open the command line and enter the download instruction
  • npm install 模块名称
  • Shorthand:npm i 模块名称
  • npm downloads the latest version by default
  • Download the specified version:npm i 模块名称@版本
  • Such as:npm install [email protected]
  • Multiple packages can be downloaded at the same time, and multiple package names are separated by spaces
    Insert picture description here
  • After the download is complete, an additional folder will be created in the current directory
  • Called node_modules
  • In this directory there will be a folder called bootstrap
  • Each download will automatically generate a file:, package-lock.jsonvarious information downloaded inside

Delete package

  • If you don’t want to use a package, you can use the command to uninstall
  • grammar:npm uninstall 包名
  • Such as:npm uninstall jquery
  • In this way, the package will be uninstalled

Manage projects

package.jsonThis file is used to describe various information about the current project. Usually a project will use a lot of packages, libraries, modules, plug-ins, etc., when the project is completed, node_modulesthis folder will be very large, if you want to go online or put it in other places to run, copy this folder If it is, it will be very big. To solve this problem, you can use it package.json. In this file, you can write the information of the packages that the project depends on, and others will know what packages the project depends on when they get this file.

  • npm will help us record the packages we are currently using in this project
  • But the premise is that you have to tell npm to say: "You will help me manage the entire folder"
  • The instructions are:npm init
  • After the command is executed, you need to enter the project information according to the prompt
  • If you don’t want to enter a lot of information, you can use all the default values:npm init -y
  • If you want to use the default, the current folder name cannot be Chinese
  • Once you have this file, if you download the package again, the dependent package information will be added to the file:
{
    
    
  "name": "03-npm",  //描述了包的名字,不能有中文
  "version": "1.0.0",  //描述了包的的版本信息, x.y.z  如果只是修复bug,需要更新Z位。如果是新增了功能,但是向下兼容,需要更新Y位。如果有大变动,向下不兼容,需要更新X位。
  "description": "", //包的描述信息
  "main": "index.js", //入口文件(模块化加载规则的时候详细的讲)
  "scripts": {
    
      //配置一些脚本,在vue的时候会用到,现在体会不到
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],  //关键字(方便搜索)
  "author": "",  //作者的信息
  "license": "ISC",  //许可证,开源协议
  "dependencies": {
    
       //重要,项目的依赖, 方便代码的共享  通过 npm install可以直接安装所有的依赖项
    "bootstrap": "^3.3.7",
    "jquery": "^3.3.1"
  }
}
  • When others get this file, they only need to execute the command to download all the packages that the project depends on:npm i

clear cache

  • Sometimes, some packages are half downloaded, but they fail for various reasons (for example, suddenly there is no internet)
  • Then the downloaded half of the package likely to be cached
  • Then when you download again in the future, it will all fail.
  • Then we have to clear the cache and download again
  • Clear npm's cache:npm cache clean -f
  • Or delete the cache folder after executing the command

Global installation

  • The packages installed above can only be used in the current folder. If you put the project files in other places, you will not find the installed dependent packages. We call this installation method partial installation.
  • There is also an installation method called global installation, which only needs to be installed once on the current computer and can be used anywhere on the computer.
  • Global installation command: npm install --global 包名 globalcan be abbreviated asg

nrm

When npm downloads the package, the default download address is abroad, and sometimes the network speed is not good. So node provides a tool to manage npm download tools, you can change the download address of npm, this tool is nrm.

Install nrm

  • The nrm tool does not come with it and needs to be downloaded and installed manually:npm i -g nrm
  • Here we need to install this nrm as a global dependency, not an internal dependency of the project anymore
  • Check whether the installation is successful:, the nrm --versionversion number can appear, indicating that the installation is successful

Use nrm

There are many websites now, and we put commonly used tools on them for people to download. We call these websites the mirror source.

  • There are several mirror source addresses in nrm, we need to pick one that is faster to use

Detect the mirror source address

  • We use the command directly on the command line to view the network speed of all mirror source addresses
  • Use of nrm:nrm test
  • Used to detect which address is faster to download, the current address will be preceded by*

Switch mirror source

  • The result of the test:, 镜像源名称 ---- 网速we choose the mirror source address with the fastest internet speed to use:
  • nrm use taobaoSwitch the download address to Taobao's mirror source

cnpm

  • Downloading can use Chinese download tools:cnpm
  • npm i cnpm -g
  • Use cnpm instead of npm, the specific usage is the same as npm

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/114546577