作用:保存当前项目所使用的模块信息,作为包描述文件,当node_modules丢失的时候,可以通过package.json文件快速恢复项目所使用的包
创建:通过npm init自动初始化
E:\前端\nodejs\实践>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: (实践) test // 创建项目名称
version: (1.0.0) 0.0.1 // 项目版本号
description: This is test // 项目描述
entry point: (index.js) main.js// 项目入口文件
test command:
git repository: // 项目github地址
keywords:
author: zt // 项目作者
license: (ISC)
About to write to E:\前端\nodejs\实践\package.json: // package.json文件
{
"name": "test",
"version": "0.0.1",
"description": "This is test",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "zt",
"license": "ISC"
}
Is this OK? (yes) yes
由于dependencies
选项,可以保存第三方包的依赖信息,如果node_modules
删除了,可以通过npm install自动把``package.json
中的dependencies
中的所有的依赖项都下载回来
package.json
文件npm install
包名的时候都加上--save
这个选项,目的是用来保存依赖项信息npm有两层含义
npm 网站
https://www.npmjs.com/npm官网,可以在该网址上上传包以及搜索已有的包,只有在此处搜索到的包才能被npm下载
npm命令行工具
只要安装了node就会安装npm,npm也有版本,可以在命令行输入以下命令获取版本号
npm --version
// 或者
npm -v
升级npm(自己升级自己)
npm install --global npm
npm init
生成package.json
文件
package.json
文件npm install
一次性把package.json
文件中dependencies
选项中的依赖项全部安装
npm uninstall 包名
npm uninstall --save 包名/ npm uninstall 包名 --save
npm --help
npm 命令 --help
原文:https://www.cnblogs.com/cheng-chen/p/14518632.html