Giter Club home page Giter Club logo

corepack's Introduction

corepack

Join us on OpenJS slack (channel #nodejs-corepack)

Corepack is a zero-runtime-dependency Node.js script that acts as a bridge between Node.js projects and the package managers they are intended to be used with during development. In practical terms, Corepack lets you use Yarn, npm, and pnpm without having to install them.

How to Install

Default Installs

Corepack is distributed by default with all recent Node.js versions. Run corepack enable to install the required Yarn and pnpm binaries on your path.

Manual Installs

Install Corepack using npm

First uninstall your global Yarn and pnpm binaries (just leave npm). In general, you'd do this by running the following command:

npm uninstall -g yarn pnpm

# That should be enough, but if you installed Yarn without going through npm it might
# be more tedious - for example, you might need to run `brew uninstall yarn` as well.

Then install Corepack:

npm install -g corepack

We do acknowledge the irony and overhead of using npm to install Corepack, which is at least part of why the preferred option is to use the Corepack version that is distributed along with Node.js itself.

Install Corepack from source

See CONTRIBUTING.md.

Usage

When Building Packages

Just use your package managers as you usually would. Run yarn install in Yarn projects, pnpm install in pnpm projects, and npm in npm projects. Corepack will catch these calls, and depending on the situation:

  • If the local project is configured for the package manager you're using, Corepack will download and cache the latest compatible version.

  • If the local project is configured for a different package manager, Corepack will request you to run the command again using the right package manager - thus avoiding corruptions of your install artifacts.

  • If the local project isn't configured for any package manager, Corepack will assume that you know what you're doing, and will use whatever package manager version has been pinned as "known good release". Check the relevant section for more details.

When Authoring Packages

Set your package's manager with the packageManager field in package.json:

{
  "packageManager": "[email protected]+sha224.953c8233f7a92884eee2de69a1b92d1f2ec1655e66d08071ba9a02fa"
}

Here, yarn is the name of the package manager, specified at version 3.2.3, along with the SHA-224 hash of this version for validation. [email protected] is required. The hash is optional but strongly recommended as a security practice. Permitted values for the package manager are yarn, npm, and pnpm.

You can also provide a URL to a .js file (which will be interpreted as a CommonJS module) or a .tgz file (which will be interpreted as a package, and the "bin" field of the package.json will be used to determine which file to use in the archive).

{
  "packageManager": "yarn@https://registry.npmjs.org/@yarnpkg/cli-dist/-/cli-dist-3.2.3.tgz#sha224.16a0797d1710d1fb7ec40ab5c3801b68370a612a9b66ba117ad9924b"
}

Known Good Releases

When running Corepack within projects that don't list a supported package manager, it will default to a set of Known Good Releases.

If there is no Known Good Release for the requested package manager, Corepack looks up the npm registry for the latest available version and cache it for future use.

The Known Good Releases can be updated system-wide using corepack install -g. When Corepack downloads a new version of a given package manager on the same major line as the Known Good Release, it auto-updates it by default.

Offline Workflow

The utility commands detailed in the next section.

  • Either you can use the network while building your container image, in which case you'll simply run corepack pack to make sure that your image includes the Last Known Good release for the specified package manager.

  • Or you're publishing your project to a system where the network is unavailable, in which case you'll preemptively generate a package manager archive from your local computer (using corepack pack -o) before storing it somewhere your container will be able to access (for example within your repository). After that it'll just be a matter of running corepack install -g --cache-only <path/to/corepack.tgz> to setup the cache.

Utility Commands

corepack <binary name>[@<version>] [... args]

This meta-command runs the specified package manager in the local folder. You can use it to force an install to run with a given version, which can be useful when looking for regressions.

Note that those commands still check whether the local project is configured for the given package manager (ie you won't be able to run corepack yarn install on a project where the packageManager field references pnpm).

corepack cache clean

Clears the local COREPACK_HOME cache directory.

corepack cache clear

Clears the local COREPACK_HOME cache directory.

corepack enable [... name]

Option Description
--install-directory Add the shims to the specified location

This command will detect where Corepack is installed and will create shims next to it for each of the specified package managers (or all of them if the command is called without parameters). Note that the npm shims will not be installed unless explicitly requested, as npm is currently distributed with Node.js through other means.

If the file system where the corepack binary is located is read-only, this command will fail. A workaround is to add the binaries as alias in your shell configuration file (e.g. in ~/.bash_aliases):

alias yarn="corepack yarn"
alias yarnpkg="corepack yarnpkg"
alias pnpm="corepack pnpm"
alias pnpx="corepack pnpx"
alias npm="corepack npm"
alias npx="corepack npx"

On Windows PowerShell, you can add functions using the $PROFILE automatic variable:

echo "function yarn { corepack yarn `$args }" >> $PROFILE
echo "function yarnpkg { corepack yarnpkg `$args }" >> $PROFILE
echo "function pnpm { corepack pnpm `$args }" >> $PROFILE
echo "function pnpx { corepack pnpx `$args }" >> $PROFILE
echo "function npm { corepack npm `$args }" >> $PROFILE
echo "function npx { corepack npx `$args }" >> $PROFILE

corepack disable [... name]

Option Description
--install-directory Remove the shims to the specified location

This command will detect where Node.js is installed and will remove the shims from there.

corepack install

Download and install the package manager configured in the local project. This command doesn't change the global version used when running the package manager from outside the project (use the `-g,--global` flag if you wish to do this).

corepack install <-g,--global> [... name[@<version>]]

Install the selected package managers and install them on the system.

Package managers thus installed will be configured as the new default when calling their respective binaries outside of projects defining the packageManager field.

corepack pack [... name[@<version>]]

Option Description
--json Print the output folder rather than logs
-o,--output Path where to generate the archive

Download the selected package managers and store them inside a tarball suitable for use with corepack install -g.

corepack use <name[@<version>]>

When run, this command will retrieve the latest release matching the provided descriptor, assign it to the project's package.json file, and automatically perform an install.

corepack up

Retrieve the latest available version for the current major release line of the package manager used in the local project, and update the project to use it.

Unlike corepack use this command doesn't take a package manager name nor a version range, as it will always select the latest available version from the same major line. Should you need to upgrade to a new major, use an explicit corepack use {name}@latest call (or simply corepack use {name}).

Environment Variables

  • COREPACK_DEFAULT_TO_LATEST can be set to 0 in order to instruct Corepack not to lookup on the remote registry for the latest version of the selected package manager, and to not update the Last Known Good version when it downloads a new version of the same major line.

  • COREPACK_ENABLE_AUTO_PIN can be set to 0 to prevent Corepack from updating the packageManager field when it detects that the local package doesn't list it. In general we recommend to always list a packageManager field (which you can easily set through corepack use [name]@[version]), as it ensures that your project installs are always deterministic.

  • COREPACK_ENABLE_DOWNLOAD_PROMPT can be set to 0 to prevent Corepack showing the URL when it needs to download software, or can be set to 1 to have the URL shown. By default, when Corepack is called explicitly (e.g. corepack pnpm …), it is set to 0; when Corepack is called implicitely (e.g. pnpm …), it is set to 1. When standard input is a TTY and no CI environment is detected, Corepack will ask for user input before starting the download.

  • COREPACK_ENABLE_UNSAFE_CUSTOM_URLS can be set to 1 to allow use of custom URLs to load a package manager known by Corepack (yarn, npm, and pnpm).

  • COREPACK_ENABLE_NETWORK can be set to 0 to prevent Corepack from accessing the network (in which case you'll be responsible for hydrating the package manager versions that will be required for the projects you'll run, using corepack install -g --cache-only).

  • COREPACK_ENABLE_STRICT can be set to 0 to prevent Corepack from throwing error if the package manager does not correspond to the one defined for the current project. This means that if a user is using the package manager specified in the current project, it will use the version specified by the project's packageManager field. But if the user is using other package manager different from the one specified for the current project, it will use the system-wide package manager version.

  • COREPACK_ENABLE_PROJECT_SPEC can be set to 0 to prevent Corepack from checking if the package manager corresponds to the one defined for the current project. This means that it will always use the system-wide package manager regardless of what is being specified in the project's packageManager field.

  • COREPACK_HOME can be set in order to define where Corepack should install the package managers. By default it is set to %LOCALAPPDATA%\node\corepack on Windows, and to $HOME/.cache/node/corepack everywhere else.

  • COREPACK_ROOT has no functional impact on Corepack itself; it's automatically being set in your environment by Corepack when it shells out to the underlying package managers, so that they can feature-detect its presence (useful for commands like yarn init).

  • COREPACK_NPM_REGISTRY sets the registry base url used when retrieving package managers from npm. Default value is https://registry.npmjs.org

  • COREPACK_NPM_TOKEN sets a Bearer token authorization header when connecting to a npm type registry.

  • COREPACK_NPM_USERNAME and COREPACK_NPM_PASSWORD to set a Basic authorization header when connecting to a npm type registry. Note that both environment variables are required and as plain text. If you want to send an empty password, explicitly set COREPACK_NPM_PASSWORD to an empty string.

  • HTTP_PROXY, HTTPS_PROXY, and NO_PROXY are supported through node-proxy-agent.

  • COREPACK_INTEGRITY_KEYS can be set to an empty string to instruct Corepack to skip integrity checks, or a JSON string containing custom keys.

Troubleshooting

Networking

There are a wide variety of networking issues that can occur while running corepack commands. Things to check:

  • Make sure your network connection is active.
  • Make sure the host for your request can be resolved by your DNS; try using curl [URL] (ipv4) and curl -6 [URL] (ipv6) from your shell.
  • Check your proxy settings (see Environment Variables).

Contributing

See CONTRIBUTING.md.

License (MIT)

See LICENSE.md.

corepack's People

Contributors

aduh95 avatar arcanis avatar austinkelleher avatar brodybits avatar das7pad avatar dependabot[bot] avatar djaler avatar gamadril avatar github-actions[bot] avatar jack-works avatar jonahsnider avatar jongwooo avatar kenrick95 avatar keysox avatar kherock avatar medhansh404 avatar merceyz avatar micsco avatar ngnlteto avatar nilock avatar paul-soporan avatar plumpnation avatar shalvah avatar shisama avatar spencerkaiser avatar stramel avatar styfle avatar teppeis avatar trott avatar zhyupe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

corepack's Issues

'corepack prepare' does not read "packageManager" in package.json

The docs and comments indicate that corepack prepare should look at the package manager configured in package.json and download it, but it currently doesn't.

Repro

  1. docker run --rm -it --entrypoint sh node:16.11.1-alpine3.14
  2. echo '{"packageManager": "[email protected]"}' > package.json
  3. ls ~/.node doesn't exist (expected)
  4. corepack prepare
  5. ls ~/.node doesn't exist (unexpected)
  6. corepack yarn --version (shows 3.0.2)
  7. ls ~/.node does exist (expected)

Manifest field

The current implementation uses engines.pm in order to define the package manager, but it's perhaps not the best location to store this information. The engines field intends to specify which versions of the package manager are capable of properly installing the package, not the project (the distinction matters: the project is whatever you clone, whereas the package is whatever people consume when running yarn add my-package).

In particular, engines are typically checked even on transitive dependencies whereas we would want the pmm definition to only be considered for the top-level manifest, and ignored for all others. It also avoids potential issues with older package managers that would print a warning with projects that would list an "unknown engine field".

Of note: package managers have their own fields in the engines field (respectively engines.npm, engines.yarn, engines.pnpm). Those would be completely ignore by pmm, which would only care about the top-level field. So, for instance, if you wish to indicate that your package needs to be installed by Yarn 1.20+ or npm 6+ and that its project needs to be developed using Yarn 1.22.4, you'd write:

{
  "packageManager": "[email protected]",
  "engines": {
    "yarn": "^1.20",
    "npm": "^6"
  }
}

Note however that I personally wouldn't recommend using engines.yarn (or any of the others) - a package should be compatible with every reasonable package manager.

Corepack needs help output

C:\Users\jasne>corepack

C:\Users\jasne>corepack --help

C:\Users\jasne>corepack --?

C:\Users\jasne>corepack -h

C:\Users\jasne>corepack /h

;-) ....

Corepack uses old yarn version after changing packageManager

How I encountered this error:

  1. Updated to nodejs v16.9.0
  2. $ sudo npm i -g npm
  3. $ sudo npm uninstall -g yarn
  4. $ corepack enable
  5. Sanity check:
    $ which yarn
    /usr/local/bin/yarn
    
    $ cat /usr/local/bin/yarn
    #!/usr/bin/env node
    require('./corepack').runMain(['yarn', ...process.argv.slice(2)]);
  6. I have an existing project with "packageManager": "[email protected]"
  7. $ yarn --version outputs 2.4.2
  8. I set packageManager to [email protected]
  9. $ yarn --version outputs 2.4.2

Actually, any valid yarn version within ^2 I set it to still yields 2.4.2. Any 1.x version of yarn seems to be honored.

Allow checking hashes for exact versions, and check them for default versions

Corepack currently depends on the security of HTTPS. Projects that want stronger guarantees should optionally be allowed to configure an integrity hash for the downloaded package manager. Obviously this would only be possible when configuring an exact version rather than a range.

Integrity hashes for the default known-good versions should be included in Corepack, so that users get the strongest security guarantees by default.

(Related to #10, but without requiring modifications to the registry or the package managers.)

Corepack included in Node 16.9.0, where is it?

Hi,
I just installed node v16.9.0 (using nvm-windows) which supposed to include corepack right off the bat. But when I open terminal and typed corepack enable, I'm getting usual error corepack : The term 'corepack' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I searched the whole drive for corepack and didn't find anything. So where is it? Do I still need to install it using npm?

Dynamic default versions

One of the main reason we adopted corepack was to avoid work in nodejs/node to support multiple package managers. There is a measurable amount of traffic on our H1 due to vulnerabilities in the npm dependencies, and we are struggling to keep npm up to date. Unfortunately corepack increases this load and it makes it even less apparent and hard to track as the vulnerabilities would not be apparent to the users.

I propose that the config file include only the major version of each package manager. This will ensure that for a specific version of Node.js there won't be any breaking change (I consider the drop of a Node.js version a breaking change).
Then we will update the major version of each package manager whenever we ship a new major release of Node.js.

I would also recommend that the config file is not bundled in but loaded at runtime. This will simplify maintenance on the Node.js side so we could just update the config file without updating corepack (and viceversa). This also enables mulitple Node.js lines to have the same version of corepack but different defaults.

Prepare the "Last" version?

The prepare command will choose "Known Good Releases" when it is not specified with a version range. Generally [email protected], [email protected] and [email protected]. I'm wondering if it can just choose the last version of them. Because Node.js 16 is installed with npm@8, it is quite surprising that corepack enable gives me npm@7.

Currently corepack prepare requires a full version to work, i.e. x.y.z. When I want to upgrade npm to 8, I have to ask npm once to get the correct version (npm view npm or npm dist-tag ls npm, and copy the latest field).

So I hope corepack would have corepack prepare --latest to just install the latest version, or maybe corepack upgrade --interactive.

Specify Version with `corepack enable` when using pnpm

Corepack version: 0.9.0

Expected behavior: when using corepack enable expect to be able to tell it which version should be enabled, much like corepack use.

Actual behavior: corepack enable does not automatically download the latest version of the package manager in question by default. Which pnpm, it downloads 6.11.0 instead of 6.14.3 (latest as of the date of this post, 8/28/2021). It does not support the version string like corepack use does, e.g. corepack enable pnpm@latest is not supported.

This is not applicable to yarn, since yarn detects the version based on the .yarn directory and what version has been registered there.

EDIT: I know that you can do the following steps (which may be somewhat redudant here) to achieve this, I think it would be more straightforward (this was not obvious to me at least) if this was done by corepack enable in this scenario.

I was able to get it to use the latest version by doing the following:

  • run corepack prepare [email protected] -o ./
  • Then running corepack hydrate ./corepack.tgz --activate
  • Finally, running corepack enable pnpm

The steps above resulted in the latest pnpm being available.

If this is in fact the desired flow for using arbitrary versions, I'm happy to help document this flow

Hardcoded references to registry.npmjs.org

Code currently hardcodes references to https://registry.npmjs.org, which makes corepack (and yarn) not usable inside a walled corporate network, even though that network might offer a local private repository for npm.

Also, even the DNS resolution of registry.npmjs.org might not be available in the walled scenario, in which case running (for example - and whichever parameter is used) yarn will return a not very informational:

Internal Error: Error when performing the request

This happens at least in the latest 0.10.0 release, with node.js v12.16.1 LTS.

Cannot install if /tmp is in a separate drive

We currently use rename, so the temporary directory can't be moved if on a different drive than where we're installing the versions. We should change the temporary directory to instead be next to the final location, or at least within the ~/.node folder (this way we're sure they're on the same drive as the destination).

Repository administration

I'm working to setup automatic updates & releases on the repository (first step was #88), but I don't have the permissions to manage the repository (in particular I need to add a secret for the npm publish step, and give the permissions for a bot user to bypass the branch protection on scheduled jobs, cf failed logs).

What's the process to do this?

cc @jasnell

Global packages

This thread aims to discuss the "global packages" scenario. For example, the pmm version of pnpx --version is currently forbidden if run within a Yarn project, even though it's supposed to run within a standalone context that might not be related.

npm install -g yarn

With pmm currently installed, the following currently happens when running npm install -g yarn:

npm ERR! code EEXIST
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/yarn/bin/yarn.js
npm ERR! dest /tmp/tmp.SfN0q4jxL7/node-v15.0.0-nightlyYYYY-MM-DDXXXX-linux-x64/bin/yarn
npm ERR! errno -17
npm ERR! EEXIST: file already exists, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/tmp/tmp.SfN0q4jxL7/node-v15.0.0-nightlyYYYY-MM-DDXXXX-linux-x64/bin/yarn'
npm ERR! File exists: /tmp/tmp.SfN0q4jxL7/node-v15.0.0-nightlyYYYY-MM-DDXXXX-linux-x64/bin/yarn
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/arcanis/.npm/_logs/2020-09-03T14_30_46_868Z-debug.log

There are a few options on the ideal behavior:

  • Ask npm to assume --force when overriding the yarn binary (or any other binary covered by pmm). Pros, it preserves the current behavior, anyone who runs npm install -g yarn can keep doing it without changing their workflows. Cons, it means that they would effectively be replacing the pmm binary, so they wouldn't benefit from the "ensures the right version" anymore.

  • Ask npm to exit with a zero exit code when it detects that the yarn binary points to pmm, and print a message explaining the situation with a link to the pmm project. Perhaps we could also make it change the default Yarn version that pmm would use for new projects. Pros, people can keep using their workflow, they don't accidentally overwrite their binaries and lose features. Cons, it's a bit magical, might be confusing to some users.

  • Decide that this is the expected behavior, and that people upgrading to Node 15.x are expected to be aware that running npm install -g yarn isn't needed anymore. Potentially ask npm to improve the error message to explain the situation. Pros, fail early, this would make this improvement noticeable. Cons, it's maybe too noticeable, and CI providers would need to adapt to this change.

For reference, the ideal message I mention would be akin to:

npm ERR! Starting from Node 15.x, Yarn is partially distributed along with Node. To check that
npm ERR! is the case, run `yarn --version` in your project directory. More information on:
npm ERR!     https://nodejs.org/pmm/notice

With the url pointing to longer explanations.

Help of corepack enable and disable mentions non-existent --bin-folder flag

"Details" section of corepack enable --help and corepack disable --help mentions --bin-folder flag:

By default it will locate the install directory by running the equivalent of
`which corepack`, but this can be tweaked by explicitly passing the install
directory via the `--bin-folder` flag.

But such a flag does not exist. I presume it was supposed to mention --install-directory?

Corepack is not recognized as an internal or external command

Node -v give me that I have Nodejs v16.14.2.
As stated in the docs,

Corepack is included by default with all Node.js installs

So that mean my environment should already have Corepack. But run corepack enable give me error it's not recognized as a command

yarn , pnpm I would like to switch between the two is it possible?

I like trying out new. IMHO That is the true test if it works for you.

One of the program I used was yarn so I had to enable it.

I would like to now use pnpm in my own project but if I run

pnpm install --silent

but it gives me

"Usage Error: This project is configured to use yarn"

It is a brand new project with nothing. I just made it with nestjs framework

nest new.

Edit:
For those who would like more explanation :)

Put this under '"name"' : in your package.json

"packageManager": "[email protected]"

Package manager detection

We were discussing with @iansu the case where package managers are detected depending on whether they are or not in the global path (@ljharb mentioned that as well in another thread). Tracking some thoughts:

  • Existing CRA & similar tutorials mention npm, so newcomers might be surprised if it gets installed via Yarn.

  • At the same time, it would be similarly confusing if tools like CRA were to always default to npm, given their current behavior.

  • If we do this Corepack thing, it's at least in part so that all package managers are on the same foot.

  • The problem only affects scaffolders, since existing projects will necessarily already have their install creations.

One potential solution would be to offer a BrowserChoice-like system under the form of a Corepack command (let's say corepack elect for now) that, when called:

  • If the command was never run before, it would print all supported package managers with an interactive interface listing their name and a link towards their respective websites. Upon selecting one of the options, Corepack would save the result inside its data folder, then would print the name of the selected package manager.

  • If the command ran before, it would simply read the data previously stored inside the data folder, and print again without prompting the user.

From the integrator perspective, the way it would work:

  • Instead of checking for Yarn's existence with things like yarn --version, the program would run corepack elect and capture the output. It would then branch depending on the result as before.

  • They could still implement their flags like --use-npm or --use-yarn, so that users can override their choice on a per-command basis.

One nice effect is that this would make it much clearer for the users which package manager they would be using. Right now, it's difficult to know whether Yarn or npm will end up being used unless 1/ you know that CRA has this behavior, and 2/ you remember whether you installed Yarn or not (you may have installed it only for another project a long time ago).

On the minus side it means that tools like CRA would have to add support for this feature, so it would require their input to know how they would feel about this workflow.

Cannot run `pnpm dist-tag react` in a pnpm configured project

pnpm dist-tag will delegate the command to npm dist-tag and cause an error.

pnpm dist-tag react
Usage Error: This project is configured to use pnpm

$ npm ...

I suggest making some commands that can be run in any project like dist-tag because they do not do anything related to the project.

npm reports that corepack has a proprietary license

$ npm view corepack

[email protected] | Proprietary | deps: none | versions: 6

bin: corepack, pnpm, pnpx, yarn, yarnpkg

dist
.tarball: https://registry.npmjs.org/corepack/-/corepack-0.6.0.tgz
.shasum: 1b839d4541176963433ecbc9f9387c2430a71b9a
.integrity: sha512-ttVd5uN2tXki35e8S6sjUt2LTRbHQUzM7iJOBZQ4XeqJMa+2PxC4IsWAs4rJ0+M6DstjnETcufj9EyunMrDFWA==
.unpackedSize: 195.6 kB

maintainers:
- arcanis <[email protected]>

dist-tags:
latest: 0.6.0  

published 5 months ago by arcanis <[email protected]>

`Internal Error: Server answered with HTTP 403`: hardcoded registries?

Getting a 403 error

$ nodenv global 17.1.0
$ npm ls -g --depth 0
/home/LOM0227/stow/nodenv/.nodenv/versions/17.1.0/lib
├── [email protected]
└── [email protected]

$ corepack enable
$ pnpm
Internal Error: Server answered with HTTP 403
    at ClientRequest.<anonymous> (/home/LOM0227/stow/nodenv/.nodenv/versions/17.1.0/lib/node_modules/corepack/dist/corepack.js:3933:31)
    at Object.onceWrapper (node:events:510:26)
    at ClientRequest.emit (node:events:390:28)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
    at TLSSocket.socketOnData (node:_http_client:487:22)
    at TLSSocket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:234:10)

I suspect these hardcoded registries have something to do with that.

const data = await httpUtils.fetchAsJson(`https://registry.npmjs.org/${spec.package}`, {headers: {[`Accept`]: `application/vnd.npm.install-v1+json`}});

const data = await httpUtils.fetchAsJson(`https://registry.npmjs.org/${spec.package}`, {headers: {[`Accept`]: `application/vnd.npm.install-v1+json`}});

This doesn't work for environments that block the public registry and require a private registry like artifactory, eg,

$ npm config list
; "user" config from /home/LOM0227/.npmrc

//[redacted].jfrog.io/artifactory/api/npm/emf-npm-virtual/:_password = (protected) 
//[redacted].jfrog.io/artifactory/api/npm/emf-npm-virtual/:username = "lmarsano@[redacted].com" 
always-auth = true 
email = "LMarsano@[redacted].com" 
registry = "https://[redacted].jfrog.io/artifactory/api/npm/emf-npm-virtual/" 

Maybe lookup registry from config files?

Node builds bundle npm and corepack together

Currently, corepack sits behind node_install_npm in node/tools/install.py

  if 'true' == variables.get('node_install_npm'):
    npm_files(action)
    corepack_files(action)

'node_install_npmis in turn set bynode/configure.py`

parser.add_argument('--without-npm',
    action='store_true',
    dest='without_npm',
    default=None,
    help='do not install the bundled npm (package manager)')
...
  o['variables']['node_install_npm'] = b(not options.without_npm)

This has the side-effect that when node is built w/o npm (configure.py --without-npm), then corepack is deselected as well. Ideally, corepack should sit behind its own configure flag (configure.py --without-corepack) instead.

Offline workflow

Some users will not want to download things dynamically. This should be addressed in the available tools.

Proposed workflow:

A command (let's say pmm hydrate) would install a tarball within the package manager cache:

pmm hydrate yarn-1.22.4.tgz
# Yarn 1.22.4 is in the cache, no dynamic install
yarn --version

How these archives will be retrieved doesn't matter: users have the responsibility for that. Some popular options will be:

  • Store them directly within the repositories
  • Download them from a private registry
  • Cache them when building the docker images

To make the tarball creation easier, pmm would include a pmm pack [email protected] command that would generate the tgz file for the given package manager (in a format suitable for pmm hydrate).

Step 1

Hello I am obviously brand new to this and I can follow along and I have all the information of what to put in As far as the codes repositories and all that but my problem is I don’t know where to put all that where do we start what do we have to open to input the codes into thank you

Support for semver ranges?

I'd like to write a package.json like:

{
  "packageJson": "pnpm@6"
}

Or maybe pnpm@^6.32.2, etc.

As it stands, adding a package manager to package.json requires a full specified version. This adds more maintenance overhead.

I think this is related to #93.

Unexpected identifier

I'm trying to clone and install dependencies in a project which appears to use corepack, and I'm having a difficult time. I've tried to follow the directions, but am getting an error. Here's what I've done.

▶ node --version
v16.13.0

▶ corepack enable

▶ corepack prepare [email protected] --activate
Preparing [email protected] for immediate activation...

▶ corepack yarn install
Syntax Error: Unexpected identifier
(function (exports, require, module, __filename, __dirname) { version https://git-lfs.github.com/spec/v1
                                                                      ^^^^^

SyntaxError: Unexpected identifier
    at new Script (node:vm:100:7)
    at NativeCompileCache._moduleCompile (/Users/ianvs/.nvm/versions/node/v16.13.0/lib/node_modules/corepack/dist/vcc.js:251:18)
    at Module._compile (/Users/ianvs/.nvm/versions/node/v16.13.0/lib/node_modules/corepack/dist/vcc.js:195:36)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (/Users/ianvs/.nvm/versions/node/v16.13.0/lib/node_modules/corepack/dist/vcc.js:170:20)
    at $K (/Users/ianvs/.node/corepack/yarn/3.0.2/yarn.js:242:4931)
    at ql (/Users/ianvs/.node/corepack/yarn/3.0.2/yarn.js:242:5785)

I guess this is an error coming from corepack? I'm not really sure what's going on, to be honest. Let me know if there is anything else I can do to help debug. Thanks!

Opt in

Hi 👋 I happened on this just meandering around the Node GH. I think this would be an excellent thing to provide with Node, but I’m concerned about some of what’s proposed.

Background

Based on my reading of README/issues clarifying progress, the current behavior is:

  1. Install corepack
  2. Use Yarn/PNPM commands as if the package manager is already installed in $PATH
  3. If it’s not, corepath silently installs and executes it with the command entered

My understanding is that this will be be bundled with Node, eliminating step 1. While convenient, that makes the following steps troubling.

Risks

Users are executing code they didn’t take any affirmative steps to install. This is a clear security issue, but it’s (subtly) more too.

In terms of security risk, it’s similar to npx prior to NPM 7:

  • It obfuscates what action the user is actually performing
  • It executes network-delivered code without auditability
  • It puts the user at the mercy of security decisions made by others
  • Network delivered code can be poisoned in a variety of ways, including network API vulnerabilities, hijacked domains, malicious detection of curl … | …

The subtly more bit: false negatives. Users or orgs might have a policy of aliasing these package manager commands to harden or restrict them for their own usage. If an environment isn’t properly provisioned, this would undo those restrictions rather than failing as expected.

Solutions/prior art

NPM 7, thankfully, introduces a prompt when using npx to ask if the user wants to download and execute commands that aren’t already available on $PATH. This isn’t a panacea, but it’s a meaningful improvement over silently downloading and executing third party code.

The same technique could be used here. The downside is it’s a minor inconvenience, but the risks it would help to alleviate are huge.

Considerations

  • Automations aren’t interactive, and may expect to be able to bypass this
  • Meaningful thought should be put into mitigation of risk around that

Feature request: mirror url

in China

❯ yarn --version
Internal Error: Error when performing the request
    at ClientRequest.<anonymous> (~/.nvm/versions/node/v16.13.0/lib/node_modules/corepack/dist/corepack.js:3937:20)
    at ClientRequest.emit (node:events:390:28)
    at TLSSocket.socketErrorListener (node:_http_client:447:9)
    at TLSSocket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Access to the binaries without global install

I wonder if it would be possible to have a feature similar to python -m pip to access the package manager binaries without having them on the path. It would be useful in case the binary name clashes with another program. That could potentially address the concern of "How Corepack scales if there's 1000 package managers added to it, each package manager adding 100 binaries to the path?".

The API could look something like:

> corepack yarn install

wdyt?

Feature request: corepack run

Add corepack run with the same behavior as [npm|yarn] run to reduce the necessity of a package manager to run user-defined scripts in the package.json. e.g. with this start script

"scripts": {
  "start": "node index.js"
}

we don't need a package manager to run:

$ corepack run start

Also, we should add the Corepack's wrappers (npm, pnpm and yarn) to the PATH automatically. Achieving the same effect like corepack enable but only on the context of the command execution to support scripts like the following:

"test": "npm run env:test && jest",

Without adding the package managers as global commands.

pnpx breaks in corepack+pnpm v7

https://github.com/pnpm/pnpm/releases/tag/v7.0.0

pnpx is now just an alias of pnpm dlx.

If you want to just execute the command of a dependency, run pnpm . For instance, pnpm eslint.

If you want to install and execute, use pnpm dlx .

Now I cannot run a pnpx command inside an npm configured project even though it installs the package in the global.

I need it because I need to run the following command:

pnpx -y npm@latest-6 install

Otherwise, npm v7+ will upgrade the lock file which is unwanted for projects that I cannot control.

I cannot use npx either because npx will always use the installed version of npm v8 and ignore the latest-6 I specified.

image

Signing of distributions

Ideally, the utility would include the requirement that package manager distributions are signed, with the ability for users to verify the signature on installation.

Corepack doesn't recognize the latest pnpm

Corepack assumes that a package manager executable file has ".js" extension:

https://github.com/nodejs/corepack/blob/main/mkshims.ts#L25
https://github.com/nodejs/corepack/blob/main/config.json#L37

But the latest pnpm executable file has ".cjs" extension:

pnpm/pnpm#3305

$ corepack prepare --activate [email protected]
Preparing [email protected] for immediate activation...
$ pnpm
node:internal/modules/cjs/loader:944
  throw err;
  ^

Error: Cannot find module '/Users/ykoma/.node/corepack/pnpm/6.3.0/bin/pnpm.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
    at Function.Module._load (node:internal/modules/cjs/loader:774:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Recommended mechanism for packaging corepack

Given that corepack is disabled by default, how does one package it for a distribution. Specifically, corepack enable creates symlinks to /usr/bin directly.

This is problematic since

  • It requires superuser access
  • It overrides the file managed by distro package manager

Is it safe to create symlinks directly to pnp[mx].js and yarn[pkg].js ? Or is this subject to change over time?

Permission issue on windows.

The node.js installer on windows (*.msi) will choose C:\Program Files\nodejs by default to place node.exe, npm and corepack. As you may know this place requires admin permission, the installer will bring up the UAC and ask for it once.

At the same time, npm (and pnpm) works well here because it will install packages to %appdata%\npm, where no permission is needed.

However, corepack will install pnpm to the same place as corepack by default (which is C:\Program Files\nodejs\node_modules\corepack. This causes permission issue:

> corepack enable
Internal Error: EPERM ....

A workaround is using --install-directory to manually change the install folder to some place without permission, for example %appdata%\corepack.

I hope corepack would use places like %appdata%\corepack on windows to avoid potential EPERM issues.

(BTW, the node.js installer for other systems are likely to choose a place with high permission. It is really an issue on macOS because it generally forbids installing anything globally, in which case users must use brew to correctly install it to a safe place. However I'm not expecting you to fix the installer logic and this is out of scope.)

Feature request: Add Yarn bin to PATH on Windows

Right out of the gate, this is my first interaction on a public repository, so feel free to throw some feedback at me.

Basically i would like for corepack to automatically add the yarn's bin directory to path, so globally installed packages can be used from PowerShell | cmd without any hassle.

This is not a behavior on windows, can't test on different OS right now.

Perhaps when the corepack enable command is used this action could be triggered, so it doesn't make any unnecessary changes for those not using corepack.

Init workflow

This thread is meant to discuss the init workflow. For example, what happens when running yarn init or npm init? Under the current model those commands will fail, because pmm is implemented in such a way that the package.json is created before spawning the process, which will then detect the preexisting manifest and refuse to update it.

Silent install is unexpected

Here's what I tried:

  • Install Node.js 16
  • Run corepack enable
  • Run pnpm --version, it prints "6.11.0"
  • Run echo '{ "packageManager": "[email protected]" }' > package.json
  • Run pnpm --version again, this time it prints "6.24.0"

I think this feature is great!

However, the fact that there is a silent installation that takes 4x longer to run pnpm --version is a bit worrisome and unexpected.

I think corepack should print something when its installing a new version so its clear that something is happening in the background. And perhaps even print where its installing to. (Related to #55)

No Windows Binaries

I run nvm-windows and have had a few users trying to use Node 16.9.0 with corepack. It's not currently clear how this will be shipping for Windows, and there don't appear to be any builds for Windows at https://github.com/nodejs/corepack/actions/runs/276463953. As it currently stands, it appears the only way to use corepack on Windows is through an npm global installation.

In case it helps, here is some background info about how version management is handled on Windows:

Currently, Windows version managers source node and their associated npm versions from https://nodejs.org/dist/index.json. The paths are derived using the zip archives from each of their respective repos (i.e. https://nodejs.org/dist/) and https://github.com/npm/cli/archive/ respectively. There is no special installation process for any version of Node. In NVM4W, a symlink is configured to point to the appropriate version, then it remains hands-off.

If corepack is going to work with version managers, it cannot be reliant on the Node installer. The Node installer is not used in version managers on Windows (this is true for Nodist as well) because it uninstalls prior versions and fails if a newer version already exists. In other words, it only allows one version to be installed at a time. See nodejs/node#4603 for details. For npm, NVM4W currently copies the npm/npx commands to the node root, making them accessible as global commands. If corepack shipped a bootstrap command like this, we could do the same thing.

Bottom line: I'm trying to figure out how to bootstrap corepack so Windows recognizes it for the active version of Node.

Status of the project

Hi,

What is the status of this project ?

We can read on the README.md

we're working with the Node TSC to provide Corepack by default starting from Node 15, thus ensuring that all package managers can be used with little to no friction.

But it seems not be embedded into Node 15 ?

There is some plan for Node 16 or more ? Or this project is it currently dead ? (last commit some months ago, and some CI tests have failed https://github.com/nodejs/corepack/runs/1227988760 )

Thanks in advance for your answer :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.