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

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

青灯夜游
青灯夜游 forward
2022-08-09 11:29:57 2364browse

npm isnode'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
    Set Taobao mirror source (Taobao mirror new domain name)
npm config set registry https://registry.npmmirror.com
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
  • Use nrm to manage the source
# 全局安装nrm npm install -g nrm # 查看nrm源列表(支持默认源,淘宝源,腾讯源等) nrm ls # 切换源(镜像源的名称nrm ls是可以看到的) nrm use taobao
    Add the .npmrc file in the project root directory, content Taobao mirror address:
registry = https://registry.npmmirror.com

Installation dependencies


  • Global installation

# 安装最新版本 npm install -g xxx # 安装指定版本 npm install -g xxx@1.0.0
    Local installation (current file folder)
npm install xxx
    Project dependencies are installed and written into
  • dependenciesofpackage.json:
npm install –S xxx # 和上面效果一样,默认: npm install xxx
    Project dependencies are installed and written into
  • devDependenciesofpackage.json:
npm install –D xxx
    Specify the version number
npm install vue@3.0.0
    This project installs dependencies. Temporarily use the Taobao image
npm install --registry=https://registry.npmmirror.com
    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

Uninstall dependencies


    ##Uninstall
  • npm uninstall 
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  npm uninstall -D 
Uninstall global dependencies
  • npm uninstall -g 
Tips
  • Alias for uninstall: remove, rm, r, un, unlink these They are all aliases of npm uninstall.
    npm uninstall -S  # 等价 npm remove -S  # 等价 npm rm -S 
Update


    Check for updates first
  • npm outdated

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
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
  • Method 2:
# 全局安装 npm-check npm install -g npm-check # 查看可更新的依赖 npm-check # 更新依赖 npm-check -u
# 更新主版本的另一种方式就是先卸载,再重新安装 # 卸载 npm uninstall xxx # 重新安装-最新版本 npm install xxx # 重新安装-制定版本 npm install xxx@2.0.0

View


    View npm version
  • npm -v
    View installed package version

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

    npm outdated
    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
  • 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
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的主页


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 文件夹存放它需要依赖的版本文件。
  • ~
不改变主版本号和次版本号,修订号可以随意更改 例如 ~2.0.0 ,可以使用 2.0.0、2.0.2 、2.0.9 的版本。
  • ^
不改变主版本号(主版本号非0),此版本号和修订号可以随意更改 例如 ^2.0.0 ,可以使用 2.0.1、2.2.2 、2.9.9 的版本。
  • *号
*表示任意版本 对版本没有限制, 一般不用 "base": "*"
  • >
大于某个版本,表示只要大于这个版本的安装包都行 例如:"node": "> 4.0.0"
  • >=
大于某个版本,表示只要大于或等于这个版本的安装包都行 例如:"node": ">= 4.0.0"
小于某个版本,表示只要小于这个版本的安装包都行 例如:"http-proxy-middleware": "
小于或等于某个版本,表示只要小于或等于这个版本的安装包都行 例如:"http-proxy-middleware": "
  • -
-表示两个版本号之间的版本 "base": "1.0.1-1.5.9" 例如 1.0.1-1.5.9 可以使用 1.0.1到1.5.9之间的任意版本

更多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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete