nodejs / corepack

Zero-runtime-dependency package acting as bridge between Node projects and their package managers
MIT License
2.57k stars 169 forks source link

Cannot use corepack in offline mode #561

Closed Qwarctick closed 1 month ago

Qwarctick commented 1 month ago

Hello,

I'm trying to use corepack in full offline mode. I got an error when running yarn where corepack try to fetch data from yarnpkg or npmjs.org even if I set the variable COREPACK_ENABLE_NETWORK=0.

I created this test case with a container without internet access after installation of corepack/yarn.

package.json

{
  "name": "js-analysis",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "jest": "^29.7.0",
    "jest-serializer-vue": "^3.1.0"
  }
}

Dockerfile

FROM node:slim

COPY package.json /src/package.json

WORKDIR /src

RUN corepack enable \
  && corepack yarn@4.x install

ENV COREPACK_ENABLE_NETWORK=0
docker network create --internal --driver bridge no-internet
docker build -f Dockerfile -t js-analysis:latest .

docker run -it --rm --network no-internet js-analysis:latest bash

root@436cdc8b9722:/src# yarn
/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21521
    throw new UsageError(`Network access disabled by the environment; can't reach npm repository ${npmRegistryUrl}`);
          ^

UsageError: Network access disabled by the environment; can't reach npm repository https://registry.npmjs.org
    at fetchAsJson2 (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21521:11)
    at fetchLatestStableVersion (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21550:26)
    at fetchLatestStableVersion2 (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21672:20)
    at Engine.getDefaultVersion (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:22292:29)
    at async Engine.executePackageManagerRequest (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:22390:47)
    at async Object.runMain (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:23096:5) {
  clipanion: { type: 'usage' }
}

Node.js v22.8.0
aduh95 commented 1 month ago

Consider setting DEBUG=corepack in your env to better understand what's happening.

Qwarctick commented 1 month ago
root@ddd6d989a9e8:/src# export DEBUG=corepack
root@ddd6d989a9e8:/src# yarn
  corepack LastKnownGood file would be located at /root/.cache/node/corepack/lastKnownGood.json +0ms
  corepack No LastKnownGood version found in Corepack home. +4ms
/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21521
    throw new UsageError(`Network access disabled by the environment; can't reach npm repository ${npmRegistryUrl}`);
          ^

UsageError: Network access disabled by the environment; can't reach npm repository https://registry.npmjs.org
    at fetchAsJson2 (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21521:11)
    at fetchLatestStableVersion (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21550:26)
    at fetchLatestStableVersion2 (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:21672:20)
    at Engine.getDefaultVersion (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:22292:29)
    at async Engine.executePackageManagerRequest (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:22390:47)
    at async Object.runMain (/usr/local/lib/node_modules/corepack/dist/lib/corepack.cjs:23096:5) {
  clipanion: { type: 'usage' }
}

Node.js v22.8.0
aduh95 commented 1 month ago
No LastKnownGood version found

That seems to be the issue. If you run corepack install -g yarn@4.x might solve that (I'm just guessing, I'm not using Docker).

where corepack try to fetch data from yarnpkg or npmjs.org even if I set the variable COREPACK_ENABLE_NETWORK=0

To clarify, I don't think that's what happens. When network is disabled by env, Corepack won't even try to fetch anything – and instead throw the error you're seeing. If you actually see some connection attempts from Corepack, that would definitely be a bug, please report it.

Qwarctick commented 1 month ago

OK thanks.

The problem seems to be that corepack does not default to the version of yarn installed by the corepack yarn@4.x install command. It tries to install yarn 1.1 and fails. It seems to need the packageManager field in the package.json. But this field doesn't seem to support syntheses like 4.X.

Qwarctick commented 1 month ago

Ok we fallback in the problem with the semver in packageManager :