Node package management tool

package management tool

A package represents a collection of source code for a specific set of functions.

Application software for managing packages, which can be used to download, install, update, delete, and upload packages. With the help of

package management tools, projects can be developed quickly and development efficiency can be improved.

Front-end commonly used package management tools

npm

Node Package Manager: node's package management tool

node will automatically install npm during installation, npm -v can view the version number test, and if the version number is displayed, the installation is successful.
insert image description here

Basic use of npm

Create an empty directory, use this directory as the working directory to start the command-line tool, execute npm init

The function of the npm init command is to initialize the folder as a package, and interactively create the package.json file

package.json is the configuration file of the package. Each package must have package.json

insert image description here

  • Precautions for the initialization process
    • The package name cannot use Chinese, university, the default is the name of the folder, so the folder name cannot be Chinese or uppercase
    • version requires xxx to be defined, x must be a number, and the default value is 1.0.0
    • ISC integers are functionally identical to MIT certificates
    • package.json can be manually created or modified
    • Use npm init -y or npm init --yes to quickly create package.json

search package name

Command line npm s/search keyword

(commonly used) website search npmjs

accumulates through practice, reads articles and projects to learn and accumulate

Download the installation package

After the npm install and npm i command installation packages

are run, two new resources will be added. The node_modules folder stores the lock file of the downloaded package

package-lock.json package, which is used to lock the version of the package

After installing uniq, uniq is a dependent package of the current package, sometimes simply referred to as dependent

eg: create a package a, install b package in a, b is a dependent package of a or a depends on b

const uniq = require('uniq');

let arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];

const result = uniq(arr);
console.log(result);

insert image description here

The basic process of require importing nnpm package

Find a folder with the same name in node_modules under the current folder

Find a folder with the same name in node_modules under the upper-level directory until you find the root directory of the disk

Production environment and development environment

The production environment is an environment specially used for writing codes. The development environment can only be accessed by programmers themselves. The production environment

is the environment where the project code is officially run. It is usually a formal server computer. The production environment is generally accessible to every customer.

production dependencies and development dependencies

Set options during installation to distinguish the type of dependencies

Production dependent commands

npm i -S uniq
npm i --save uniq
-S is equivalent to –save, -S is the default option

Package information is saved in the dependencies property in package.json

Development dependent commands

npm i -D less
npm i --save-dev less
-D is equivalent to –save-dev

package information is saved in the devdependencies attribute in package.json.




Development dependencies are dependent packages used only in the development phase, while production dependencies are dependent packages used in both the development phase and the final online operation phase.

npm install globally

Global installation

npm i -g nodemon

After the global installation, you can execute the nodemon command anywhere on the command line (automatically restart the node application)

illustrate

  • Globally installed commands are not affected by the working location directory
  • You can view the location of the global installation package through npm root -g
  • Not all packages are suitable for global installation, only global tools are suitable, you can check the official documentation of the package to determine the installation method
    insert image description here

Modify windows execution policy

Windows does not allow npm global commands to execute script files by default, so the execution policy needs to be modified

  • Open the powershell command line as an administrator
  • Type the command set-ExecutionPolicy remoteSigned

Right-click on the bottom of win10 to search to display the search box, enter powershell in the search box, and right-click to run as an administrator.
insert image description here

Test after modifying the execution strategy
insert image description here

Or change the type of command line, or modify it to another way
insert image description here
insert image description here
insert image description here

Environment variable Path

Assume that our current user name for logging in to the operating system is: zhangsan

Then we need to add the c:\users\zhangsan\go\bin path to the PATH variable. Under Windows, we can execute it in PowerShell:

$env:Path += ";c:\users\zhangsan\go\bin"

Note that this is only a temporary modification of the current PATH variable. When we close and reopen PowerShell, we need to reset it.

If you want to make permanent changes, you need to start PowerShell with administrator privileges, and then execute:

setx /M PATH "%PATH%;c:\users\zhangsan\go\bin"

All dependencies of the npm installation package

npm i can rely on package.json and package-lock.json to declare that the installation directory depends on

npm i

npm install

In most cases, the node_modules folder will not be stored in the repository, because it is too large.
Generally, after pulling the project, there is no such folder. The project cannot be run, and all dependencies need to be installed. npm i is very important.
some common commands

  • Install global dependencies npm i
  • Install the specified version of the package npm [email protected]
  • remove dependencies
  • partial deletion
  • npm remove uniq
  • npm r uniq
  • global delete
  • npm remove -g nodemon

npm configuration command aliases

Configure the script property in package.json

  "yy": "code ./index.js",
    "start": "code ./index.js"

After the configuration is complete, you can use the alias to execute the command, and then run it on the cmd terminal (the powershell operation reports an error)

npm run yy
npm  run start

insert image description here

run can be omitted when the start alias is used

npm start

Supplementary note:

  • npm start is a common command in the project, generally used to start the project.
  • npm run has the feature of automatically searching to the superior directory, which is the same as the require function (it can be run even if it is not the current folder directory)
  • For unfamiliar projects, we can refer to some operations of the project by viewing the scripts property.

yarn

JavaScript Package Management Tool
Features: super fast installation, super safe and reliable.
npm install yarn

npm i -g yarn

Yarn common commands

  • Initialize yarn init / yarn init -y
  • Installation package yarn add uniq production dependency/yarn add less --dev development dependency/yarn global add nodemon global installation
  • Delete package yarn remove uniq delete project dependency package/yarn global add nodemon global delete package
  • The installation project depends on yarn
  • Run the command alias yarn name
    After installing yarn, an error is reported in the vscode terminal initialization:
    Error: Cannot find or load the main class init

    View some articles and modify the jdk of java (the version is still the same), and finally found that it can be normal in the cmd of the system In progress

    insert image description here

Globally installed packages are not available, and the location of yarn global installed packages can be viewed through yarn global bin.

The choice of npm and yarn

yarn configuration Taobao mirror

yarn config set registry https://registry.npmmirror.com/

You can view yarn configuration items through yarn config list
insert image description here

personal project

random choice

company project

Choose according to the project code. You can judge the package management tool of the project through the lock file

. The lock file of npm is package-lock.json.

The lock file of yarn is yarn.lock.

Remember not to mix package management tools

cnpm

cnpm is a complete image of npmjs.com built by Taobao. It is also called Taobao mirror. The
cnpm server is deployed on the domestic Alibaba Cloud server Sag, which can improve the download speed of packages.
The official also provides a global toolkit cnpm, and the operation commands are roughly the same as npm.
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-cjTZZk64-1686623908418)(11.png)]

Install

Install cnpm tools through npm

npm install -g cnpm --registry=https://registry.npmmirror.com

operation command

  • Initialize cnpm init
  • Install package cnpm i uniq;cnpm i -S uniq;cnpm i -D uniq;cnpm i -g nodemon;
  • The installation project depends on cnpm i
  • remove cnpm r uniq

Npm configuration Taobao mirror

You can also use Taobao mirroring with npm, and there are two ways to configure it

  • Directly configure
    npm install -g cnpm --registry=https://registry.npmmirror.com
  • tool configuration
  1. Use nrm to configure the mirror address of npm npm registry manager
  2. Modify mirror

    nrm use taobao
  3. Check whether the configuration is successful (optional)
    npm connfig list
    Check whether the register address is npmmirror, if yes, it indicates success
    Supplementary notes:
    1. It is recommended to use tool configuration for mirror configuration, because subsequent modification is more convenient
    . Although cnpm can improve the speed, npm It can be accelerated through Taobao mirroring, so the usage rate of npm is still higher than that of cnpm

npm publishes a package

Create and publish

Publish the toolkit developed by yourself to the npm service for the convenience of yourself and other developers.

  1. Create a folder, create a file index.js, declare functions in the file, and use modules.exports to expose
function add(a, b) {
 return a + b;
 }
// 暴露
 module.exports = {
   add
}
  1. npm initialization toolkit, package.json fills in the information of the package (the name of the package is unique)
  2. Register an account on npmjs
  3. Modify to the official official image (run nrm use npm on the command line)
  4. Fill in the relevant user information in npm login under the command line
  5. Submit package with npm publish under command line

update package

Subsequent updates can be made to the published package

  1. Update the code in the package
  2. Test whether the code is available
  3. Modify the version number of package.json
  4. post update

npm publish

### 删除包
npm unpublish删除包
前提:那你是包的作者,发布小于24小时;发布大于24小时,没有其他包依赖,并且每周小于300下载量,并且只有一个维护者
## 包管理工具nvm
用来管理node版本的工具,方便切换不同版本的Node.js<br/>
### 常用命令
nvm list available 显示所有可以下载的Node.js版本<br/>
nvm on       #启用版本管理
nvm off      #禁用版本管理

nvm ls                  #查看本地 Node 版本
nvm ls-remote           #查看官网 Node 版本
nvm ls-remote --lts     #查看官网 Node LTS 版本

nvm current                 #显示当前的版本
nvm install [node版本号]     #安装指定版本
nvm uninstall [node版本号]   #卸载指定版本
nvm use [node版本号]         #使用指定版本

nvm alias default [node版本号]    #设置默认使用版本

nvm alias [别名] [node版本号]      #给不同的版本号添加别名
nvm unalias [别名]                #删除已定义的别名

nvm install-latest-npm                       #在当前node版本中,将npm升级到最新版
卸载指定版本
nvm use [node版本号]         #使用指定版本

nvm alias default [node版本号]    #设置默认使用版本

nvm alias [别名] [node版本号]      #给不同的版本号添加别名
nvm unalias [别名]                #删除已定义的别名

nvm install-latest-npm                       #在当前node版本中,将npm升级到最新版
nvm reinstall-packages [node版本号]           #在全局重新安装npm,从[node版本号]版本到当前版本

Guess you like

Origin blog.csdn.net/weixin_55355282/article/details/131183494