Npm and yarn install specified version packages, delete dependencies; quickly delete node_modules files

Install the specified version package (the format is 'package name@version number')

npm version: npm install --save package name@version number

yarn version: yarn add package name@version number

Example: Install element-ui version 2.15.8

npm 版:npm install --save [email protected]

yarn 版: yarn add [email protected]

After installation, you can see in package.json: "element-ui": "^2.15.8"

Note: In the case of an existing version, you need to delete the dependency first, and you cannot directly change the version number in package.json, because the version number before modification is still in package-lock.json

remove dependencies

npm version: npm uninstall package-name

yarn version: yarn remove package-name

Example: delete element-ui dependency

npm 版:npm uninstall element-ui

yarn 版:yarn remove element-ui

After deletion, there is no "element-ui": "^2.15.8" in package.json

installation type

Global installation (dependencies will be downloaded and installed into the [Global Directory], and will be automatically installed when creating a new project to install dependencies)

npm version: npm install package name --global // Shorthand: npm install package name -g

yarn version: yarn global add package-name

Local installation (download dependencies to the directory project where the current command line is located)

npm version: npm install package-name

yarn version: yarn add package-name

Write information to package.json during installation

npm install xxx --save // ​​Shorthand: npm install xxx -S or npm install xxx
npm install xxx --save-dev // Shorthand: npm install xxx -D

yarn add xxx --dev // Shorthand: yarn add xxx -D

yarn add xxx --peer // Shorthand: yarn add xxx -P

yarn add xxx --optional // Shorthand: yarn add xxx -O

// --save adds the name of the dependent package to the package.json file dependencies, the things that are still dependent after release
// --save-dev adds the name of the dependent package to the package.json file devDependencies, the things that are depended on during development

// --peer adds the dependent package name to the package.json file peerDependencies

// --optional Add the dependent package name to the package.json file under optionalDependencies

Command comparison between npm and yarn

illustrate Yarn NPM/CNPM
initialize a project yarn init npm init
Install dependencies by default yarn install/link npm install/link
Install a dependency and save it to package by default yarn add taco npm install taco --save
remove a dependency yarn remove taco npm uninstall taco --save
Install a development-time dependency yarn add taco -dev npm install taco --save -dev
Update a dependent project yarn upgrade taco npm update taco --save
Install a global dependency project yarn global add taco npm install taco --global
Publish/login/logout, a series of NPM operations yarn publish/login/logout npm publish/login/logout
run a command yarn run/test npm run/test

How to quickly delete the node_modules file

Install the rimraf package globally
Function: wrap the rm -rf command in the form of a package, used to delete files and folders, no matter whether the folder is empty or not, it can be deleted

npm i rimraf -g

Enter the project that needs to be cleaned up and execute the following command

rimraf node_modules

Guess you like

Origin blog.csdn.net/AdminGuan/article/details/128302166