Node.js Package Manager (Corepack)

 

Table of contents

1 Introduction

2. Enable Corepack

3. Use Node.js Corepack

4. Configuration package

5. Upgrade the global version

6. Offline workflow

7. Supported package managers

8. Node.js Corepack intercepts npm

9. Corepack common commands


1 Introduction

Corepack is an experimental tool that helps manage package manager versions. It exposes binary proxies for each supported package manager that, when invoked, will recognize the package manager configured for the current project, can be explicitly installed if desired, and finally run without explicit user interaction .

This feature simplifies two core workflows:

  • It simplifies the barrier of entry for new contributors, as they no longer need to follow a system-specific installation process, they just need to have the package manager you want.
  • It allows you to ensure that everyone on your team will be using the version of the package manager you want them to use without them having to manually sync every time you need to make an update.

2. Enable Corepack

Since it is still experimental, it is not enabled by default and requires us to explicitly enable it to use it. It will set the symbolic link in the environment next to the binary file (and overwrite the existing symbolic link if necessary). To enable it, run the following command:

corepack enable

From now on, any calls to supported binaries will work without further setup. You can also disable it if you run into problems by running the following command:

corepack disable

3. Use Node.js Corepack

I have installed two versions of Node locally, and check whether the corresponding version has Corepack.

For Node multi-version installation, you can also refer to:

http://t.csdn.cn/8Ci3jicon-default.png?t=N5K3http://t.csdn.cn/8Ci3j

Check the directory of the next version (V14.4.0) and find no corepack, as shown below:

Check the directory of the next version (V16.16.0) and find no corepack, then manually enable corepack and run the command:

corepack enable

The directory is shown in the figure below:

 We can see that there are. When the corepack enable command is executed, we find that there are more pnpm and yarn-related files below.

You can also view the following corepacks and their corresponding versions through the command line:

C:\Users\Administrator>corepack -v
0.17.0

4. Configuration package

The Corepack proxy will find the closest package.json file in your directory. The current directory hierarchy to extract its "packageManager" property.

If the value corresponds to a supported package manager
, Corepack will make sure all calls to the binaries in question are for the requested version, downloading it on demand if necessary, or aborting if it cannot be retrieved successfully.

For example, the following configuration:

{
  "name": "test_yilai",
  "version": "1.0.0",
  "packageManager": "[email protected]",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "element-plus": "^2.3.7",
    "element-ui": "^2.15.13",
    "lodash": "^4.17.21"
  }
}

After configuration, try to install the following dependencies:

yarn install 
Usage Error: This project is configured to use pnpm

If packageManager is changed to yarn

{
  "name": "test_yilai",
  "version": "1.0.0",
  "packageManager": "[email protected]",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "element-plus": "^2.3.7",
    "element-ui": "^2.15.13",
    "lodash": "^4.17.21"
  }
}

Look at the results of pnpm execution: 

pnpm instal

Usage Error: This project is configured to use yarn

In order to solve this problem, we can use the package installation artifact (ni) to solve this problem. Execute the following command to install:

npm i -g @antfu/ni

After completion, enter ni directly on the command line and press Enter to install the package, as shown in the following figure:

We can find the perfect one for installation. . . . . .

5. Upgrade the global version

When running outside of an existing project (for example, while running yarn init ), Corepack will default to using predefined versions corresponding to the latest stable releases of each tool. These versions can be set along the package manager version you wish to set by running the corepack prepare command: 

corepack prepare [email protected] --activate

Alternatively, labels or ranges can be used:

corepack prepare pnpm@latest --activate
corepack prepare yarn@stable --activate

6. Offline workflow

Many production environments do not have network access. Since Corepack usually downloads package manager distributions directly from their registries, it may conflict with such environments. To avoid this from happening, call the corepack prepare command while you still have network access (usually while preparing the image for deployment). This will ensure that the required package managers are available even without network access.

7. Supported package managers

The following binaries are provided via Corepack:

Package manager

Binary names

yarn

yarnyarnpkg

pnpm

pnpmpnpx

8. Node.js Corepack intercepts npm

While Corepack can support npm like any other package manager, it's not enabled by default.

Although npma "packageManager"valid option in the property, it does not take effect.

Of course, if you want to intercept it is also possible, you need to solve it through corepack enable npm .

9. Corepack common commands

corepack enable Turn on the corepack function

corepack disable remove corepack function

corepack hydrate imports the package manager into the cache

corepack prepare Generate a package manager archive

 

Guess you like

Origin blog.csdn.net/u014388408/article/details/131484361