npm common commands + commonly used front-end package management tools and npm Taobao image configuration, etc.
1 Introduction
1.1 Download and install NodeJs
- download link
- Official website:
https://nodejs.org/en . - Chinese website:
https://nodejs.org/zh-cn .
- Official website:
- After installation, use
node -v
the command to view the installed version, as follows:
1.2 on windows
- Regarding the installation on Windows, you can refer to the following article:
Getting Started with vue2 - Novice Level 1 .
1.3 Common package management tools
- Commonly used package management tools on the front end include: npm, cnpm, yarn, please continue for more...
2. npm
- Commonly used front-end package management tools include: npm, yarn, cnpm
2.1 npm installation
- The full name of npm is
Node Package Manager
(ie: node's package management tool). npm is the official built-in package management tool of node.js, so npm will be automatically installed when installing NodeJs. If NodeJs is already installed, you can check the installation version through the command, asnpm -v
follows :
2.2 npm initialization package
- The initialization command is as follows:
- Interactive command:
npm init
, as follows:
- Quickly create commands (created by default), as follows:
npm init -y 或 npm init --yes
- Interactive command:
2.3 npm install and uninstall packages
2.3.1 Non-global installation
2.3.1.1 Installation of a single package
2.3.1.1.1 Default version installation
- For example, if you want to use date format now, there is a js
dayjs.min.js
. If you want to use it, you can download it directly, and then import it into html, as follows:
https://www.bootcdn.cn/ .
- If you want to use it in a project after node is initialized, install it directly, as follows:
- Go to the following address to search for the corresponding package:
https://www.npmjs.com/
- Then copy the installation command and execute it:
npm i dayjs 或者 npm install dayjs
- After installation, it can be used by importing it into js, as follows:
// 导入 dayjs 的包 const dayjs_2 = require('dayjs'); // 测试,使用 dayjs // 注意:dayjs_1 要与上面定义的保持一致(const dayjs_1) var date = dayjs_2(Date.now()).format('YYYY-MM-DD');
- Go to the following address to search for the corresponding package:
2.3.1.1.2 Specified version installation
-
grammar:
npm i <包名@版本号>
-
Install or update as follows:
npm i [email protected]
Automatically update if installed:
2.3.1.2 Install all packages
-
The new down project will need many packages. You can execute the following command to install all the dependencies required for the project according to the dependency declarations of
package.json
and , as follows:package-lock.json
npm i #懒人版 或 npm install # 码农版
2.3.1.3 Uninstall package
- The following commands are available:
npm remove dayjs npm r dayjs npm rm dayjs
2.3.2 npm global installation and uninstallation
- grammar:
- Coder version:
npm install --global 包名
- Lazy version:
npm i -g 包名
- Install the typings global package, command:
npm i -g typings
, I am on a Mac and have permission issues, so I added itsudo
as follows:sudo npm i -g typings
- Coder version:
- View installed global packages
npm ls -g #查看所有 npm ls -g typings #查看已安装的指定包
- Update global package
npm update -g 包名 sudo npm update -g typings # Mac
- Uninstall global package
npm remove -g 包名 npm rm -g 包名 sudo npm rm -g typings
- Check the directory where the global package is located:
npm root -g
2.4 npm configuration command alias
test.js
The execution command we executed above can be configured with an alias and executed with the npm command, as follows:- First
package.jso
configure in the n file"exeTest":"node ./test.js "
- Execute to see the effect:
npm run exeTest
- First
2.5 npm configure Taobao image
2.5.1 Direct configuration using commands
- URL: npmmirror mirror site .
- The command is as follows:
npm config set registry https://registry.npmmirror.com
2.5.2 Configuration using nrm tool
- First, install
nrm
(npm registry manager), the command is as follows:sudo npm i -g nrm
- View supported mirror addresses:
nrm ls
- According to the supported mirror address, select the mirror address to switch:
nrm use taobao # 切换淘宝镜像
- Check the current mirror address:
npm config list
3. cnpm
3.1 Installation and simple use of cnpm
-
URL: npmmirror mirror site .
-
Installation command:
sudo npm install -g cnpm --registry=https://registry.npmmirror.com
-
After installation, check the version:
cnpm -v
-
The use of cnpm initialization, installation package, uninstallation package, etc. is the same as that of npm, as follows, etc., no more details will be said.
cnpm init cnpm i [email protected]
4. yarn
- yarn installation:
sudo npm i -g yarn
- about yarn
- It’s fast, safe and reliable. For more information, please go to yarn’s official website:
https://yarnpkg.com/ - For specific commands, please refer to the official website: https://yarnpkg.com/getting-started/usage .
5. Summary
5.1 npm common commands
5.1.1 npm initialization package
- as follows:
npm init npm init -y # 快速创建(默认)
5.1.2 Install package command (single package)
- grammar:
npm i 包名 或 npm install 包名
- Production dependencies:
wherenpm i -S dayjs 或 npm i --save dayjs
-S
is equivalent to--save
,-S
which is the default option. - Development dependencies:
Whichnpm i -D dayjs 或 npm i --save-dev dayjs
-D
is equivalent to--save-dev
. - Default installation:
If no option is added, it will be installed in production dependency mode by default.
5.1.3 Install all packages
- For newly downloaded projects, the following are required:
npm i #懒人版 或 npm install # 码农版
5.1.4 Update and uninstall package commands
- Specified version update:
npm i [email protected]
- Uninstall command:
npm remove dayjs npm r dayjs npm rm dayjs
5.1.5 npm global related commands
- as follows:
npm ls -g #查看所有已经安装的全局包 npm ls -g typings #查看已安装的指定包 npm root -g # 查看全局包的安装目录 sudo npm i -g typings #安装 sudo npm update -g typings # 更新 sudo npm remove -g typings # 卸载 sudo npm rm -g typings # 卸载
5.2 Common commands related to nrm
- View supported mirror addresses:
nrm ls
- According to the supported mirror address, select the mirror address to switch:
nrm use taobao # 切换淘宝镜像
- Check the current mirror address:
npm config list