Skip to main content

npm的使用方法

· 2 min read

什么是npm

一、npm是一个包管理工具,类似后端的 maven, gradle

首先,需要使用npm就得先安装nodejs,安装完成之后呢,nodejs是会自带 npm 的。

  • npm 安装的依赖都会下载到 node_modules目录下面

如果创建package.json?

初始化一个package.json

npm init -y   npm init --yes 
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"hello": "hello world"
},
"keywords": [],
"author": "",
"license": "MIT"
}

package.json 这是一个json文件,用来管理这些包的版本和一些依赖关系,当项目中缺少 node_modules目录时,执行 npm installnpm i 这些 依赖就会一个一个的下载下来

script 中定义的 命令 可以通过以下方法来运行

npm run hello

配置默认的 package.json

npm set init.license 'MIT'
npm set init.author 'xxx'
...

安装 - 卸载 - 更新

#以下两种方式均可安装
npm i vue
npm install vue #安装一个依赖,这个命令能直接把包下载下来,放在node_modules目录下

#指定版本安装
npm i jquery@3.0.0

#安装开发版本
npm i --save-dev vue #这种安装方式将只会在开发环境中用到,生成环境不会

#卸载依赖
npm uninstall vue

#如果需要从package.json中删除依赖,需要加上 --save参数
npm uninstall --save lodash

#更新依赖
npm update vue

npm 自身升级

npm install npm@latest -g