npm init | 会引导你创建一个package.json文件,包括名称、版本、作者这些信息等 |
npm install <name> |
安装nodejs的依赖包 例如npm install express 就会默认安装express的最新版本 也可以通过在后面加版本号的方式安装指定版本,如npm install express@3.0.6 |
npm install <name> -g | 将包安装到全局环境中 |
npm install <name> --save |
安装的同时,将信息写入package.json中 任选其一即可:[--save|--save-dev|--save-optional]
--save: Package will appear in your dependencies. --save-dev: Package will appear in your devDependencies. --save-optional: Package will appear in your optionalDependencies. |
npm remove <name> | 移除安装包 |
npm update <name> | 更新安装包 |
npm ls | 列出当前安装的了所有包 |
npm root | 查看当前包的安装路径 |
npm root -g 查看全局的包的安装路径
npm help 帮助,如果要单独查看install命令的帮助,可以使用的npm help install
实战:
1、我们先创建一个文件夹:test(测试使用)
2、进入test目录 npm init
Press ^C at any time to quit. name: (test) test version: (0.0.0) 0.0.1 description: this is for testting entry point: (index.js) index.js test command: node index.js git repository: keywords: author: license: (ISC) About to write to D:\happy\node\test\package.json: { "name": "test", "version": "0.0.1", "description": "this is for testting", "main": "index.js", "scripts": { "test": "node index.js" }, "author": "", "license": "ISC" }
3、目录下会自动生成一个:package.json
4、安装自己所需的依赖包:npm install <name> --save
"dependencies": { "mysql": "^2.5.2" }