Using npm and yarn

Time:2022-9-21

Continuing to organize

Steps to initialize the library:

npm init -y
npm install webpack --save-dev

View modules

All modules installed globally

npm list -g

All modules installed in the current folder

npm list

View the specified module

npm list <packageName>

npm configuration items

View configuration list

npm config ls
npm config list

Set cache folder

The default address of the cache file: C:UsersAdministratorAppDataRoamingnpm-cache
The default address of the global package file: C:UsersAdministratorAppDataRoamingnpmnode_modules

# set cache
npm config set cache "D:\npm\node_cache"
# set global
npm config set prefix "D:\npm\node_global"

set configuration proxy

npm config set proxy null
npm config set proxy <url>

npm add user

Register users in the resource library (register by email)

npm adduser

publish module

npm publish

Create a file

touch

update module

npm update

npm update <packageName>

yarn update

  • Download npm-check-updates
  • yarn upgrade-interactive –latest (recommended)
  • yarn upgrade [email protected]
// download first
yarn global add npm-check-updates
// Update the package (yarn.lock and package.json are updated synchronously)
ncu --upgrade --upgradeAll && yarn upgrade

yarn upgrade-interactive --latest
// Need to manually select the upgraded dependency package, press the space bar to select, a key to switch all, i key to invert the selection

yarn upgrade [email protected]
// Both yarn.lock and package.json will be updated, but version locked &quot;echarts&quot;: &quot;4.2.0-rc.2&quot;

uninstall install

npm uninstall <packageName>

search module

npm search <packageName>

Installation package

Select the installation mode according to the parameters:

  • Add -g to install globally
  • Add -D or –save-dev to install as a toolkit, the package does not participate in packaging
  • Add -S or –save to install as a dependency package, which is packaged by the packaging tool
npm install <packageName>
npm i <packageName>
npm i -g <packageName>
npm i -D <packageName>
npm i --save-dev <packageName>
npm i --save <packageName>

If the following error occurs:
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087

The solution is: npm config set proxy null

local installation

npm i --save  <packageName>

Install globally

Default address for global installation

npm i -g <packageName>

cache

clear cache

npm cache clean

Set cache folder

The default address of the cache file: C:UsersAdministratorAppDataRoamingnpm-cache
The default address of the global package file: C:UsersAdministratorAppDataRoamingnpmnode_modules

# set cache
npm config set cache "D:\npm\node_cache"
# set global
npm config set prefix "D:\npm\node_global"

Or you can modify the file configuration.npmrc on the folder: C:UsersAdministrator

Note: If you modify the directory location of the global package folder, you need to add it to the computer environment variable Path,Global installation directory D: npmnode_global

npm mirror

View the current mirror:

npm config get registry

npm config get registry
yarn config get registry

Set permanent mirror source:

npm config set registry

npm config set registry http://registry.npm.taobao.org/
npm config set registry https://registry.npmjs.org/
yarn config set registry https://registry.npm.taobao.org/

When using Taobao source or try cnpm

Install cnpm package

npm install -g cnpm --registry=https://registry.npm.taobao.org/
cnpm install <packageName>

When using cnpm -v/cnpm -version to view the version, it prompts &quot;cnpm command prompt is not an internal or external command&quot;

Reason: The local system did not add the environment variables of the npm module, so the command could not be recognized.

Solution: Add the global installation directory D:npmnode_global to the environment variable

Set up a temporary mirror:

npm –registry

npm --registry https://registry.npm.taobao.org install <packageName>
yarn --registry https://registry.npm.taobao.org add <packageName>

FAQS

Global package operation is an error: SecurityError: (:) [], PSSecurityException


+ CategoryInfo          : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess

Reason: The default permission level of PowerShell under Windows is Restricted, which does not allow the execution of PS scripts (ie .ps1 files). If you run with Restricted permission level, you will get the above error

解决:Set-ExecutionPolicy RemoteSigned -Scope process

  • Popular science window execution strategy:
#update execution strategy
Set-ExecutionPolicy <policy-name>

#policy-name possible values
Unrestricted、RemoteSigned、AllSigned、Restricted、Default、Bypass、Undefined

#Change ExecutionPolicy to Unrestricted to run unsigned scripts
Set-ExecutionPolicy Unrestricted

#Query detailed strategy
get-help about_Execution_Policise

node-sass installation error

First of all, it is necessary to rule out whether the node-sass version conflicts with your installed py2 and py3. If there is no conflict, do the following.

Excluding the py problem, it is generally a problem of unstable domestic network.

Then we have several common methods:

  • Set the Taobao source of a specific package:

npm config set sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
npm config set phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
npm config set electron_mirror=https://npm.taobao.org/mirrors/electron/
npm config set registry=https://registry.npm.taobao.org

The above operations can be automatically downloaded from Taobao source when node-sass, electron and phantomjs are installed using npm install.

  • Use cnpm Taobao source to download

npm install -g cnpm

cnpm install

  • use a VPN

npm config set proxy (http://127.0.0.1:1080) here is the proxy address of the VPN

npm i node-sass

Remove the http proxy after the download is complete

npm config delete proxy