1. Introduction
1. What is NPM
NPM stands for Node Package Manager. It is a Node.js package management tool and the world's largest module ecosystem. All modules in it are open source and free; it is also a Node.js package management tool, which is equivalent to the front-end Maven.
2. Installation location of NPM tools
We can easily download the js library through npm and manage the front-end project.
The location of the npm packages and tools installed by Node.js by default: Node.js directory\node_modules
In this directory, you can see the npm directory, npm itself is a tool managed by the NPM package manager, indicating that Node.js has integrated the npm tool
#在命令提示符输入 npm -v 可查看当前npm版本
npm -v
Two, use npm to manage projects
1. Create a folder npm
2. Project initialization
#建立一个空文件夹,在命令提示符进入该文件夹 执行命令初始化
npm init
#按照提示输入相关信息,如果是用默认值则直接回车即可。
#name: 项目名称
#version: 项目版本号
#description: 项目描述
#keywords: {Array}关键词,便于用户搜索到我们的项目
#最后会生成package.json文件,这个是包的配置文件,相当于maven的pom.xml
#我们之后也可以根据需要进行修改。
#如果想直接生成 package.json 文件,那么可以使用命令
npm init -y
2. Modify the npm mirror
NPM official management packages are all downloaded from http://npmjs.com, but this website is very slow in China.
It is recommended to use Taobao NPM mirror http://npm.taobao.org/. Taobao NPM mirror is a complete npmjs.com mirror. The synchronization frequency is currently every 10 minutes to ensure that it is synchronized with the official service as much as possible.
设置镜像地址:
#经过下面的配置,以后所有的 npm install 都会经过淘宝的镜像地址下载
npm config set registry https://registry.npm.taobao.org
#查看npm配置信息
npm config list
3. Use of npm install command
#使用 npm install 安装依赖包的最新版,
#模块安装的位置:项目目录\node_modules
#安装会自动在项目目录下添加 package-lock.json文件,这个文件帮助锁定安装包的版本
#同时package.json 文件中,依赖包会被添加到dependencies节点下,类似maven中的 <dependencies>
npm install jquery
#npm管理的项目在备份和传输的时候一般不携带node_modules文件夹
npm install #根据package.json中的配置下载依赖,初始化项目
#如果安装时想指定特定的版本
npm install [email protected]
#devDependencies节点:开发时的依赖包,项目打包到生产环境的时候不包含的依赖
#使用 -D参数将依赖添加到devDependencies节点
npm install --save-dev eslint
#或
npm install -D eslint
#全局安装
#Node.js全局安装的npm包和工具的位置:用户目录\AppData\Roaming\npm\node_modules
#一些命令行工具常使用全局安装的方式
npm install -g webpack
4. Other commands
#更新包(更新到最新版本)
npm update 包名
#全局更新
npm update -g 包名
#卸载包
npm uninstall 包名
#全局卸载
npm uninstall -g 包名
Use the second way to download jquery dependencies
using npm
. Use the third way to use package. json file download dependency
Use the command npm install
This is to import the dependency in package.json first
, and then use the npm install command in the current project.