npm common commands + commonly used front-end package management tools and npm Taobao image configuration, etc.

1 Introduction

1.1 Download and install NodeJs

1.2 on windows

1.3 Common package management tools

  • Commonly used package management tools on the front end include: npm, cnpm, yarn, please continue for more...

2. npm

  • Commonly used front-end package management tools include: npm, yarn, cnpm

2.1 npm installation

  • The full name of npm is Node Package Manager(ie: node's package management tool). npm is the official built-in package management tool of node.js, so npm will be automatically installed when installing NodeJs. If NodeJs is already installed, you can check the installation version through the command, as npm -vfollows :
    Insert image description here

2.2 npm initialization package

  • The initialization command is as follows:
    • Interactive command: npm init, as follows:
      Insert image description here
    • Quickly create commands (created by default), as follows:
      npm init -ynpm init --yes
      
      Insert image description here

2.3 npm install and uninstall packages

2.3.1 Non-global installation

2.3.1.1 Installation of a single package

2.3.1.1.1 Default version installation
  • For example, if you want to use date format now, there is a js dayjs.min.js. If you want to use it, you can download it directly, and then import it into html, as follows:
    https://www.bootcdn.cn/ .
    Insert image description here
  • If you want to use it in a project after node is initialized, install it directly, as follows:
    • Go to the following address to search for the corresponding package:
      https://www.npmjs.com/
      Insert image description here
    • Then copy the installation command and execute it:
      npm i dayjs
      
      或者
      
      npm install dayjs
      
      Insert image description here
      Insert image description here
    • After installation, it can be used by importing it into js, ​​as follows:
      // 导入 dayjs 的包
      const dayjs_2 = require('dayjs');
      
      // 测试,使用 dayjs
      // 注意:dayjs_1 要与上面定义的保持一致(const dayjs_1)
      var date = dayjs_2(Date.now()).format('YYYY-MM-DD');
      
      Insert image description here
2.3.1.1.2 Specified version installation
  • grammar:

    npm i <包名@版本号>
    
  • Install or update as follows:

    npm i [email protected]
    

    Automatically update if installed:
    Insert image description here

2.3.1.2 Install all packages

  • The new down project will need many packages. You can execute the following command to install all the dependencies required for the project according to the dependency declarations of package.jsonand , as follows:package-lock.json

    npm i  #懒人版npm install  # 码农版
    

    Insert image description here

2.3.1.3 Uninstall package

  • The following commands are available:
    npm remove dayjs
    
    npm r dayjs
    
    npm rm dayjs
    

2.3.2 npm global installation and uninstallation

  • grammar:
    • Coder version:
      npm install --global 包名
      
    • Lazy version:
      npm i -g 包名
      
    • Install the typings global package, command: npm i -g typings, I am on a Mac and have permission issues, so I added it sudoas follows:
      sudo npm i -g typings
      
      Insert image description here
  • View installed global packages
    npm ls -g  #查看所有
    npm ls -g typings  #查看已安装的指定包
    
  • Update global package
    npm update -g 包名
    
    sudo npm update -g typings  # Mac
    
  • Uninstall global package
    npm remove -g  包名
    npm rm -g  包名
    
    sudo npm rm -g typings
    
    Insert image description here
  • Check the directory where the global package is located:
    npm root -g
    
    Insert image description here

2.4 npm configuration command alias

  • test.jsThe execution command we executed above can be configured with an alias and executed with the npm command, as follows:
    • First package.jsoconfigure in the n file
      "exeTest":"node ./test.js "
      
      Insert image description here
    • Execute to see the effect:
      npm run exeTest
      
      Insert image description here

2.5 npm configure Taobao image

2.5.1 Direct configuration using commands

  • URL: npmmirror mirror site .
  • The command is as follows:
    npm config set registry https://registry.npmmirror.com
    
    Insert image description here

2.5.2 Configuration using nrm tool

  • First, install nrm(npm registry manager), the command is as follows:
    sudo npm i -g nrm
    
    Insert image description here
  • View supported mirror addresses:
    nrm ls
    
    Insert image description here
  • According to the supported mirror address, select the mirror address to switch:
    nrm use taobao   # 切换淘宝镜像
    
  • Check the current mirror address:
    npm config list
    
    Insert image description here

3. cnpm

3.1 Installation and simple use of cnpm

  • URL: npmmirror mirror site .

  • Installation command:

    sudo npm install -g cnpm --registry=https://registry.npmmirror.com
    
  • After installation, check the version:

    cnpm -v
    

    Insert image description here

  • The use of cnpm initialization, installation package, uninstallation package, etc. is the same as that of npm, as follows, etc., no more details will be said.

    cnpm init
    cnpm i [email protected]
    

4. yarn

5. Summary

5.1 npm common commands

5.1.1 npm initialization package

  • as follows:
    npm init
    npm init -y   # 快速创建(默认)
    

5.1.2 Install package command (single package)

  • grammar:
    npm i 包名  
    或 
    npm install 包名
    
  • Production dependencies:
    npm i -S dayjs
    
    或
    
    npm i --save dayjs
    
    where -Sis equivalent to --save, -Swhich is the default option.
  • Development dependencies:
    npm i -D dayjs
    
    或
    
    npm i --save-dev dayjs
    
    Which -Dis equivalent to --save-dev.
  • Default installation:
    If no option is added, it will be installed in production dependency mode by default.

5.1.3 Install all packages

  • For newly downloaded projects, the following are required:
    npm i  #懒人版npm install  # 码农版
    

5.1.4 Update and uninstall package commands

  • Specified version update:
    npm i [email protected]
    
  • Uninstall command:
    npm remove dayjs
    
    npm r dayjs
    
    npm rm dayjs
    

5.1.5 npm global related commands

  • as follows:
    npm ls -g  #查看所有已经安装的全局包
    npm ls -g typings  #查看已安装的指定包
    
    npm root -g  # 查看全局包的安装目录
    
    sudo npm i -g typings   #安装
    
    sudo npm update -g typings   # 更新
    
    sudo npm remove -g typings   # 卸载
    sudo npm rm -g typings   # 卸载
    

5.2 Common commands related to nrm

  • View supported mirror addresses:
    nrm ls
    
  • According to the supported mirror address, select the mirror address to switch:
    nrm use taobao   # 切换淘宝镜像
    
  • Check the current mirror address:
    npm config list
    

Guess you like

Origin blog.csdn.net/suixinfeixiangfei/article/details/132500366