npm
node package manager
npm网站
npm命令行工具
npm就是一个命令行工具,只要安装了node就安装了npm
npm也有版本的概念 通过npm --version查看
升级npm install --global npm
常用命令
npm init初始化,创建package.jsonnpm init -y可以跳过向导,快速升级npm install 包名下载包npm install 包名 --save下载并保存到package.json依赖项npm install一次性把package.json里面保存的依赖项全部安装npm uninstall 包名只删除,依赖信息会保存npm uninstall --save 包名连着依赖信息一起删除npm help查看npm使用帮助install可以直接使用i代替--save可以使用-S代替
npm被墙的问题
npm install --global cnpm下载安装npm淘宝镜像
使用cnpm安装就可以直接从国内镜像拉取依赖
package.json
我们建议每个项目根目录都要有一个package.json文件,包描述文件,记录项目导入了哪些依赖
这个文件可以通过npm init的方式来自动初始化出来
在安装依赖的时候在后面加上 --save 该依赖就会自动记录到package.json文件中
如果node_modules丢失了,只需要使用npm install指令就可以找回所有依赖项
例如 npm install jquery --save
PS C:\Users\27843\Desktop\image> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (image)
version: (1.0.0) 0.0.1
description: 使用
entry point: (index.js) main.js
test command:
git repository:
keywords:
author: ylf
license: (ISC)
About to write to C:\Users\27843\Desktop\image\package.json:
{
"name": "image",
"version": "0.0.1",
"description": "使用",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ylf",
"license": "ISC"
}
yarn
yarn可以更好用的npm,yarn将已安装的库保存到本地,以后再次安装会先检查本地是否有对应依赖,如果有,直接引用本地依赖即可,速度相较于npm更快,yarn不需要使用--save命令,yarn自动保存依赖记录到文件中
必须要先安装npm再安装yarn
安装
npm install yarn --global
查看版本
yarn -v
初始化项目
yarn init
安装依赖
yarn add package
升级依赖
yarn upgrade package
移除依赖
yarn remove package
安装项目所有依赖
yarn
yarn install