一、概念
二、npm使用
初始化npm
// 初始化 npm 手动配置 package.json 文件
npm init
// 快速创建 npm 默认配置 package.json 文件
npm init -y
搜索包网址:https://www.npmjs.com
下载安装npm
npm install 包名
或
npm i 包名
使用包
const test = require("包名")
环境依赖
// 生产环境依赖:
方式一:npm i -S 包名
方式二:npm i --save 包名
// 存在于 package.json 中的 dependencies 属性
// 开发环境依赖:
方式一:npm i -D 包名
方式二:npm i --save-dev 包名
// 存在于 package.json 中的 devDependencies 属性
全局安装
npm i -g 包名
安装指定版本包
npm i jquery@1.0.0
删除包
// 方式一:
npm r jquery
// 方式二:
npm remove jquery
// 方式三(全局删除):
npm remove -g jquery
配置命令别名
- package.json 中的 scripts 属性
{
"scripts": {
"serve": "node server.js",
"start": "node index.js"
}
}
- 启动命令:
npm run serve
npm run start
安装cnpm
npm i -g cnpm --registry=https://registry.npmmirror.com
三、yarn使用
// 全局安装
npm i -g yarn
// 初始化
yarn init
// 安装包
yarn add 包名
// 开发环境安装 less 依赖包
yarn add less --dev
// 删除依赖包
yarn remove 包名
// 启动项目
yarn serve
yarn start