Home > Web Front-end > JS Tutorial > [Compilation and Sharing] Common npm commands necessary for front-end development

[Compilation and Sharing] Common npm commands necessary for front-end development

青灯夜游
Release: 2022-08-09 11:31:54
forward
2462 people have browsed it

npm is node's default package management tool. In front-end development, being familiar with npm's common commands will be of great help to us in solving problems. The following article will share with you some common npm instructions. I hope it will be helpful to you!

[Compilation and Sharing] Common npm commands necessary for front-end development

npm source settings


    ##Query the current source configuration
npm get registry
Copy after login
    Set Taobao mirror source (Taobao mirror new domain name)
npm config set registry https://registry.npmmirror.com
Copy after login
The old http://npm.taobao.org and http://registry.npm.taobao.org domain names will be used in 2022 The service will be suspended from 0:00 on May 31st. So don’t use the old command:


npm config set registry http://registry.npm.taobao.org

    Restore the default source
npm config set registry https://registry.npmjs.org
Copy after login
  • Use nrm to manage the source
# 全局安装nrm
npm install -g nrm
# 查看nrm源列表(支持默认源,淘宝源,腾讯源等)
nrm ls
# 切换源(镜像源的名称nrm ls是可以看到的)
nrm use taobao
Copy after login
    Add the .npmrc file in the project root directory, content Taobao mirror address:
registry = https://registry.npmmirror.com
Copy after login

Installation dependencies


  • Global installation

 # 安装最新版本
 npm install -g xxx
 # 安装指定版本
 npm install -g xxx@1.0.0
Copy after login
    Local installation (current file folder)
npm install xxx
Copy after login
    Project dependencies are installed and written into
  • dependencies of package.json:
npm install –S xxx 
# 和上面效果一样,默认:
npm install xxx
Copy after login
    Project dependencies are installed and written into
  • devDependencies of package.json:
npm install –D xxx
Copy after login
    Specify the version number
npm install vue@3.0.0
Copy after login
    This project installs dependencies. Temporarily use the Taobao image
npm install --registry=https://registry.npmmirror.com
Copy after login
    to install a certain dependency package (the following command specifies version @1.0.0). Temporarily Use Taobao mirror:
npm install XXX@1.0.0 --registry=https://registry.npmmirror.com
Copy after login

Uninstall dependencies


    ##Uninstall
  • npm uninstall <package-name></package-name>
    Copy after login
Uninstall and remove in package.json:
    Using the -S or --save flag, this operation will also remove the reference in the package.json file;
  • If the package is development Dependencies (listed in devDependencies in the package.json file) must be removed from the file using the -D or --save-dev flag.

    npm uninstall -S <package-name>
    npm uninstall -D <package-name></package-name></package-name>
    Copy after login
Uninstall global dependencies
  • npm uninstall -g <package-name></package-name>
    Copy after login
Tips
  • Alias ​​for uninstall: remove, rm, r, un, unlink these They are all aliases of npm uninstall.
    npm uninstall -S <package-name>
    # 等价
    npm remove -S <package-name>
    # 等价
    npm rm -S <package-name></package-name></package-name></package-name>
    Copy after login
Update


    Check for updates first
  • npm outdated
    Copy after login

The packages marked in red are those that can be updated, and those marked in yellow are those that cannot be updated. [Compilation and Sharing] Common npm commands necessary for front-end development

Perform update (only the patch version or minor version will be updated, the major version will not be updated)
    If you are not sure about the major and minor versions, there is a version number description below, please pull down.

  • npm update
    Copy after login
Update the main version
  • # 1.安装"npm-check-updates"模块  
    npm install -g npm-check-updates  
    # 2.检查可更新的模块  
    ncu  
    npm-check-updates  
    # 以上两条命令都可检查可更新模块。接下来更新package.json的依赖包到最新版本:  
    
    #升级 package.json 文件的 dependencies 和 devDependencies 中的所有版本
    ncu -u  
    #以上命令执行,更新全部模块。但在实际开发中不建议一次全部更新,可以根据实际需要,更新指定的模块,并且可以根据作用范围在后面加上 -D、-S 或 -g
    Copy after login
  • Method 2:
# 全局安装 npm-check
npm install -g npm-check
# 查看可更新的依赖
npm-check
# 更新依赖
npm-check -u
Copy after login
# 更新主版本的另一种方式就是先卸载,再重新安装
# 卸载
npm uninstall xxx
# 重新安装-最新版本
npm install xxx
# 重新安装-制定版本
npm install xxx@2.0.0
Copy after login

View


    View npm version
  • npm -v
    Copy after login
    View installed package version

  • npm 套件名称 -v
    Copy after login
    View npm help
  • npm help
    Copy after login
    View all folders used by npm
  • npm help folders
    Copy after login
    View the module list (view the list of locally installed modules)
  • npm list
    # 也可以使用缩写 ls
    npm ls
    Copy after login
    View the globally installed packages
  • npm list -g --depth 0
    Copy after login
View the version of a package
  • npm list vue-cli
    Copy after login
    View the package.json folder of the node module
  • npm view moduleNames
    Copy after login
View the dependencies of the package
  • npm view moudleName dependencies
    Copy after login
    View the source file address of the package
  • npm view moduleName repository.url
    Copy after login
    Check the version of Node that the package depends on
  • npm view moduleName engines
    Copy after login
    Check the installation path of the current package
  • npm root
    Copy after login
    View the installation path of the global package
  • npm root -g
    Copy after login
    Check whether the package is outdated
  • This command will list all outdated packages , you can update the package in time

    npm outdated
    Copy after login
    Access npm's json folder
  • An npm package is a folder that contains package.json , package.json describes the structure of this folder. The method of accessing npm's json folder is as follows

    npm help json
    Copy after login
  • This command will open a web page in the default way. If the default opening program is changed, it may not open as a web page.

    Check whether the package name already exists
  • When publishing an npm package, you need to check whether a certain package name already exists

    npm search packageName
    
    # 也可以使用縮写 s 來替代 search 
    npm s packageName
    Copy after login
npm Other commands

npm cache clean  # 清除npm的缓存
npm prune  # 清除项目中没有被使用的包
npm outdated  # 检查模块是否已经过时
npm repo jquery  # 会打开默认浏览器跳转到github中jquery的页面
npm docs jquery  # 会打开默认浏览器跳转到github中jquery的README.MD文件信息
npm home jquery  # 会打开默认浏览器跳转到github中jquery的主页
Copy after login


npm dependent package version number

##npm version number format X.Y.Z

means: major version number. minor version number. revision number. The increment rules of the version number are as follows:

X. Major version number: when you do Incompatible API modification,
  • Y. Minor version number: When you make a backward compatible functional addition,
  • Z . Revision number: when you make a backwards compatibility fix.

依赖库版本号、符号

  • 没有任何符号
1.0.0
完全百分百匹配,当前库/项目必须使用当前版本号,如果和其他依赖使用了相同库不同版本,会在库的文件夹下建立一个 node_modules 文件夹存放它需要依赖的版本文件。
Copy after login
  • ~
不改变主版本号和次版本号,修订号可以随意更改
例如 ~2.0.0 ,可以使用 2.0.0、2.0.2 、2.0.9 的版本。
Copy after login
  • ^
不改变主版本号(主版本号非0),此版本号和修订号可以随意更改
例如 ^2.0.0 ,可以使用 2.0.1、2.2.2 、2.9.9 的版本。
Copy after login
  • *号
*表示任意版本 对版本没有限制, 一般不用
"base": "*"
Copy after login
  • >
大于某个版本,表示只要大于这个版本的安装包都行
例如:"node": "> 4.0.0"
Copy after login
  • >=
大于某个版本,表示只要大于或等于这个版本的安装包都行
例如:"node": ">= 4.0.0"
Copy after login
  • <
小于某个版本,表示只要小于这个版本的安装包都行
例如:"http-proxy-middleware": "<0.17.3"
Copy after login
  • <=
小于或等于某个版本,表示只要小于或等于这个版本的安装包都行
例如:"http-proxy-middleware": "<=0.17.3"
Copy after login
  • -
-表示两个版本号之间的版本
"base": "1.0.1-1.5.9"
例如 1.0.1-1.5.9 可以使用 1.0.1到1.5.9之间的任意版本
Copy after login

更多node相关知识,请访问:nodejs 教程

The above is the detailed content of [Compilation and Sharing] Common npm commands necessary for front-end development. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template