The English word for package is package, which stands for a collection of source code for a particular function.
Package management application that allows you to download, install, update, delete and upload packages.
Rapid development of projects with the help of package management tools to improve development efficiency
Commonly used front-end package management tools are npm, yarn, cnpm
npm
npm is automatically installed during installation.
Basic npm usage
-
initialization
The npm init command is used to initialize a folder as a package, interactively create files
is the configuration file for the package, and every package must have the
npm init
After that, the command line is entered interactively at the prompt, and after it's all run, it creates the
{ "name": "test", // package name "version": "1.0.0", // package version "description": "test", // package description "main": "", // Package entry file "scripts": { // script configuration "test": "echo \"Error: no test specified\" && exit 1" }, // script configurations "author": "", // author "license": "ISC" // Open Source Certificate }
You can use npm init -y or npm init --yes to create packages very quickly. The package name should not be in Chinese or uppercase, and the default value is the folder name if you don't write it.
-
Search Toolkit
# command line npm search keyword
You can also go tonpm package URLlook for sth.
-
Download the installation package
npm install package name npm i package name # After installation, the installed package is a dependency of the current package.
After running it, two resources will be added to the folder
- node_modules folder Stores downloaded packages.
- The package lock file, which is used to lock the version of the package.
-
The basic process of importing an npm module with require
const fs = require('fs')
- Look for the corresponding folder with the same name in the current folder node_modules
- If it is not found, look for a folder with the same name in node_modules in the parent directory until you find the disk root.
Development dependency vs. production dependency
-
To produce dependencies, package information is stored in the dependencies property of the
npm --save package name npm i -S package name # abbreviation
-
Development dependencies, package information is stored in the devDependencies property of the
npm i --save dev package name npm -D package name #short
-
Development dependencies are packages that are only used during the development phase, while production dependencies are packages that are used during both the development phase and the final go-live phase.
dependency package
npm global install
The global install command is independent of the location of the working directory, and you can view the location of the globally installed packages with npm root -g
npm i package name # local install, package can only be used in the corresponding working directory
npm i -g package name # global installation
# nodemon serves to automatically restart Node applications
npm i -g nodemon
nodemon # runs js files instead of node, so you don't need to restart the service manually when the code changes.
Install all dependencies
This command installs project dependencies based on the dependency declarations for and
npm install
npm i # abbreviation
Install the specified version of the package
npm i package name @ version number
Delete Package
npm remove package name
npm r package name #short
npm r -g package name #global remove
Configuring Command Aliases
Commands can be executed more easily by configuring command aliases, and the scripts attribute in the
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "server": "node .
"server": "node . /" // Configure the command alias
},
# Execute this command, it will go to scripts to find the corresponding Server, and then execute the Server's code node run code
npm run server
If you configure the start attribute in scripts, you don't need to add run when running, just npm start
npm start is a common command used to start a project.
npm run has a feature that automatically looks up in the parent directory, just like the require function.
cnpm
cnpm is a full mirror of the Taobao build, also known as Taobao Mirror, at /etc/cnpm.com
The cnpm service is deployed on the domestic AliCloud servers, which can increase the download speed of packages.
There is also a global toolkit, cnpm, that does much the same thing as npm.
Installing cnpm
# Install cnpm
npm install -g cnpm --registry=
cnpm initialization, add, delete, change, and other operations commanded in the same way as npm
npm configure taobao mirrors
-
Direct Configuration
npm config set registry /
-
Tool configuration, use nrm to configure npm's mirror address
# Install nrm npm i -g nrm # Modify the image nrm use taobao # Check if the configuration was successful npm config list
yarn
yarn is a new Javascript package management tool introduced by Facebook in 2016.official website
Features.
Speed: yarn caches each downloaded package, so there's no need to re-download it when you use it again. It also utilizes parallel downloads to maximize resource utilization, resulting in faster installations!
Security: yarn algorithmically verifies the integrity of each installed package before executing the code.
Reliable: Using a detailed, concise lock file format and a well-defined installation algorithm, yarn is guaranteed to work flawlessly on different systems.
-
Installing yarn
npm i -g yarn
-
initialization
yar init -y # quick initialization
yar init -y # Fast initialization
-
Installation of dependencies
yarn add package name # Production dependencies yarn add package name --dev # Development dependencies yarn global add package name # Global installation
-
Remove dependencies
yarn remove package name # local remove yarn global remove package name # Remove globally
-
Install project dependencies
yarn
-
The configuration is the same, run with the following command
yarn Alias
-
Configuring Taobao mirrors with yarn
# This can be done by yarn config list ferret out yarn configuration item yarn config set registry /
-
The lock file for npm is ,the lock file for yarn is
Managing Release Packages
You can publish your own toolkits to the npm service for your own use and the use of other developers.
- Create the folder and create the file , declare the function in the file, and expose it using the
- npm initializes the package, filling in the package information (the package name is unique)
- Register for an account/signup
- Activate account
- Modify to the official official image (run nrm use npm on the command line)
- command line npm login Fill in the relevant user information
- Command-line npm publish commits packages
- Modify the package: Modify the version number in the package, and then use npm publish to release the update.
- Packages can be removed using npm unpublish --force