npm
包管理工具安装node默认自带npm
package.json
- 生成
# 会弹出一堆问题,我们可以输入对应内容,也可以使用默认值。
npm init
# 跳过问题,直接生成默认文件
npm init --yes
- 文件结构
- name:项目/模块名称,长度必须小于等于214个字符,不能以"."(点)或者"_"(下划线)开头,不能包含大写字母。
- version:项目版本。
- author:项目开发者,它的值是你在https://npmjs.org网站的有效账户名,遵循“账户名<邮件>”的规则,例如:zhangsan zhangsan@163.com。
- description:项目描述,是一个字符串。它可以帮助人们在使用npm search时找到这个包。
- keywords:项目关键字,是一个字符串数组。它可以帮助人们在使用npm search时找到这个包。
- private:是否私有,设置为 true 时,npm 拒绝发布。
- license:软件授权条款,让用户知道他们的使用权利和限制。
- bugs:bug 提交地址。
- contributors:项目贡献者 。
- repository:项目仓库地址。
- homepage:项目包的官网 URL。
- dependencies:生产环境下,项目运行所需依赖。
- devDependencies:开发环境下,项目所需依赖。
- scripts:执行 npm 脚本命令简写,比如 “start”: “react-scripts start”, 执行 npm start 就是运行 “react-scripts start”。
- bin:内部命令对应的可执行文件的路径。
- main:项目默认执行文件,比如 require(‘webpack’);就会默认加载 lib 目录下的 webpack.js 文件,如果没有设置,则默认加载项目跟目录下的 index.js 文件。
- module:是以 ES Module(也就是 ES6)模块化方式进行加载,因为早期没有 ES6 模块化方案时,都是遵循 CommonJS 规范,而 CommonJS 规范的包是以 main 的方式表示入口文件的,为了区分就新增了 module 方式,但是 ES6 模块化方案效率更高,所以会优先查看是否有 module 字段,没有才使用 main 字段。
- eslintConfig:EsLint 检查文件配置,自动读取验证。
- engines:项目运行的平台。
- browserslist:供浏览器使用的版本列表。
- style:供浏览器使用时,样式文件所在的位置;样式文件打包工具- parcelify,通过它知道样式文件的打包位置。
- files:被项目包含的文件名数组。
// 例子
{
"name": "react", // 包名
"version": "1.0.0", // 版本号
"description": "Command line instructions", // 包的描述信息,将会在npm search的返回结果中显示,以帮助用户选择合适的包。
"keywords": [ // 包的关键词信息,是一个字符串数组,同上也将显示在npm search的结果中。
"react",
"es6",
"react with es6"
],
"private": true, // 设为true这个包将不会发布到NPM平台下。
"homepage": "https://github.com/rainnaZR/es6-react", // 包的主页地址。
"bugs": { // 包的bug跟踪主页地址。
"url": "https://github.com/rainnaZR/es6-react",
"email": "111@163.com"
},
"license": "ISC", // 包的开源协议名称。
"author": "ZRainna", // 包的作者。
"main": "src/pages/index.js", // 包的入口文件。
"directories": { // CommonJS包所要求的目录结构信息,展示项目的目录结构信息。字段可以是:lib, bin, man, doc, example。值都是字符串。
"tests": "tests",
"lib":"lib",
"docs":"docs"
},
"repository": { // 包的仓库地址。
"type": "git",
"url": "git+https://github.com/rainnaZR/es6-react.git"
},
"scripts": { // 通过设置这个可以使NPM调用一些命令脚本,封装一些功能。
"start": "babel-node src/pages/index.js",
"build": "webpack --config config/webpack.config.js",
"watch": "webpack-dev-server --config config/webpack.config.js --hot --inline --progress"
},
"babel": { // babel
"presets": [
"es2015-node5"
]
},
"devDependencies": { // 开发依赖
"webpack": "^1.13.2",
"webpack-dev-server": "^1.16.1"
},
"dependencies": { // 生成依赖
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"react-redux": "^4.4.5",
"react-router": "^2.8.1",
"redux": "^3.6.0"
}
}
- init 命令设置一些默认值,比如:
npm set init.author.email "邮箱@qq.com"
npm set init.author.name "姓名"
npm set init.license "MIT" // 这里是指开源协议
使用
- 检测是否安装成功
npm -v
# 8.4.1 成功显示版本号
- 模块安装
# 安装 package.json里的依赖
npm install
# 安装指定模块
npm install <name>
# 安装指定版本
npm install <name>@<version>
# 安装指定tag的版本
npm install <name>@<tag>
# 安装模块指定版本号范围
npm install <name>@<version range>
# npm install axios@">=0.2.0 <0.2.9"
# 强制拉取远程资源,即使本地已经安装这个模块
npm install <name> --force
# 全局安装
npm install -g|--global <name>
# 安装依赖 dependencies
npm install <name>
npm install -S <name>
npm install -save <name>
# 安装依赖 devDependencies
npm install -D <name>
npm install --save-dev <name>
错误信息:npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 解决:npm config set proxy null
- 获取设置全局安装默认目录
# 获取
npm config get prefix
# 设置
npm config set prefix "directory"
- 显示npm的bin目录
npm bin
- 设置npm配置
# npm config set <key> <value> [--global]
# 设置代理
npm config set proxy=http://proxy.tencent.com:8080
# 设置镜像
npm config set registry http://npm.oa.com
# 淘宝镜像
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
npm config set disturl https://mirrors.huaweicloud.com/nodejs/
npm config set electron_mirror https://mirrors.huaweicloud.com/electron/
npm config set registry https://registry.npm.taobao.org
npm config set disturl https://npm.taobao.org/dist
npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
- 获取npm配置
# npm config get <key>
# 获取npm当前镜像地址
npm config get registory
- 删除npm配置
# npm config delete <key>
# 删除代理设置
npm config delete proxy
- 在编辑器中打开npm配置文件
npm config edit
- 删除模块
# 全局添加 -g
npm rm <name>
npm r <name>
npm uninstall <name>
npm un <name>
- 查找模块
npm search [search terms ..]
npm s [search terms ..]
npm se [search terms ..]
- 查看安装信息
# 全局添加 -g
npm list
npm ls
- 查看模块版本号
# 全局添加 -g
npm list <name>
npm ls <name>
- 更新模块
# 全局添加 -g
npm update <name>
- 运行脚本
npm run <name>