作用
帮助你安装模块(包),自动安装依赖,管理包(增,删,更新,项目所有包)
安装到全局环境
- 安装到电脑系统环境下
- 使用时在任何位置都可以使用
- 被全局安装的通常是:命令行工具,脚手架
npm i 包名 -g 安装
npm uninstall 包名 -g 卸载
安装到项目环境
只能在当前目录使用,需要使用npm代运行
初始化项目环境
npm init
初始化npm管理文件package.json
package-lock.json 文件用来固化依赖
{
"name": "npm", //项目名称
"version": "0.0.1", //版本
"description": "test and play", //描述
"main": "index.js", //入口文件
"dependencies": { //项目依赖 上线也要用
"jquery": "^3.2.1"
},
"devDependencies": { //开发依赖 上线就不用
"animate.css": "^3.5.2"
},
"scripts": { //命令行
"test": "命令行",
},
"repository": { //仓库信息
"type": "git",
"url": "git+https://github.com/alexwa9.github.io/2017-8-28.git"
},
"keywords": [ //关键词
"test",'xx','oo'
],
"author": "wan9",
"license": "ISC", //认证
"bugs": {
"url": "https://github.com/alexwa9.github.io/2017-8-28/issues"//问题提交
},
"homepage": "https://github.com/alexwa9.github.io/2017-8-28#readme"//首页
}
项目依赖
只能在当前项目下使用,上线了,也需要这个依赖 --save
//安装
npm i 包名 --save
npm install 包名 -S
npm install 包名@x.x.x -S
//卸载
npm uninstall 包名 --save
npm uninstall 包名 -S
开发依赖
只能在当前项目下使用 ,上线了,依赖不需要了 --save-dev
npm install 包名 --save-dev
npm install 包名 -D
查看包
npm list 列出所有已装包
npm outdated 版本对比(安装过得包)
npm info 包名 查看当前包概要信息
npm view 包名 versions 查看包历史版本列表
安装所有依赖
npm install
安装package.json里面指定的所有包
选择源
npm install nrm -g 安装选择源的工具包
nrm ls 查看所有源
nrm test 测试所有源
nrm use 切换源名
安装卡顿时
ctrl + c -> npm uninstall 包名 -> npm cache 清除缓存 -> 换4g网络 -> npm install 包名
发布包
- 官网 注册
- 登录
npm login
登录- 输入 user/password/email
- 创建包
npm init -y
- 创建入口index.js
- 编写,输出
- 发布
npm publish
- 迭代
- 修改版本号
npm publish
- 删除
npm unpublish
包的发布、迭代、删除,需要在包目录下进行
删除包,有时需要发送邮件