npm常用指令

安装: npm install <name>

npm install <name>

安装依赖包,默认安装最新版本,也可在后面加上版本号,并且将安装信息加入项目的package.json中

eg: npm install express@3.0.6

npm install <name> --global, npm install <name> -g

全局环境安装,通过require()无法调用,需要修改环境变量

npm install <name> --save, npm install <name> -S

安装至node_modules中并将安装信息写入dependencies(生产阶段依赖)中

eg:

"dependencies": {
    "gulp": "^3.9.1"
}

npm install <name> --save-dev, npm install <name> -D

安装至node_modules中并将安装信息写入devDependencies(开发阶段依赖)中

eg:

"devDependencies": {
    "gulp": "^3.9.1"
}

npm install <name> --save-optional, npm install <name> -O

安装至node_modules中并将安装信息写入optionalDependencies(可选阶段依赖)中

eg:

"optionalDependencies": {
    "gulp": "^3.9.1"
}

npm install <name> --save-exact, npm install <name> -E

精确安装至指定版本,默认最新

eg:

"dependencies": {
    "gulp": "3.9.1"
}
  

可以发现"^"消失了

npm install

模块的依赖写入package.json后,将其加入根目录,输入npm install进行安装将文件的安装信息内的模块全部安装

----------------------------------------------------------------------------------------------------------------------------------

卸载:npm uninstall/remove/rm/r/unlink/un <name> <dependency>

命令行内加入要删除的模块名以及其依赖方式即可,有多种写法

-----------------------------------------------------------------------------------------------------------------------------------

更新至最新: npm undate <name> <dependency>

不输入名称,依赖时全部更新

-----------------------------------------------------------------------------------------------------------------------------------

新建package.json: npm init

字段解读:

name: 包名

version: 版本号

description:包的描述

homePage:包的官网url

author:包的作者

contributors:其他贡献者名

dependencies 依赖包列表,如果列表内的包尚未安装,npm会自动将其安装在node_modules下

repository:包代马存放的地方

main:main 字段是一个模块ID,它是一个指向你程序的主要项目

keywords:关键字,方便搜索

原文地址:https://www.cnblogs.com/yanze/p/6084848.html