nodejs / corepack

Zero-runtime-dependency package acting as bridge between Node projects and their package managers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

this[kClient].connect is not a function

marinedayo opened this issue · comments

Error when downloading yarn with proxy settings.

Details with NODE_DEBUG are below:

bash-5.1# corepack -v
0.26.0
bash-5.1# NODE_DEBUG=fetch HTTPS_PROXY=http://127.0.0.1:9999/ yarn version
FETCH 96: connecting to registry.npmjs.org using https:h1
FETCH 96: connection to registry.npmjs.org using https:h1 errored - this[kClient].connect is not a function
FETCH 96: request to GET https://registry.npmjs.org//yarn errored - this[kClient].connect is not a function
Internal Error: Error when performing the request to https://registry.npmjs.org/yarn; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting
    at fetch (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22878:11)
    at async fetchAsJson (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22892:20)
    at async fetchLatestStableVersion (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22942:20)
    at async fetchLatestStableVersion2 (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22975:14)
    at async Engine.getDefaultVersion (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:23537:25)
    at async Engine.executePackageManagerRequest (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:23644:30)
    at async BinaryCommand.validateAndExecute (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:21164:22)
    at async _Cli.run (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22139:18)
    at async Object.runMain (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:24371:12)

this[kClient].connect is not a function error has occurred.

  • v0.26.0 (latest release): error
  • v0.25.2 (bundled by Node.js 20.12.0): error
  • v0.23.0 (bundled by Node.js 20.11.1): no error

corepack imports undici/lib/proxy-agent.js to use ProxyAgent, but connect function is not found here.

https://github.com/nodejs/corepack/blob/v0.26.0/sources/httpUtils.ts#L66-L81
https://github.com/nodejs/undici/blob/v6.6.2/lib/proxy-agent.js
https://github.com/nodejs/undici/blob/v6.6.2/lib/dispatcher-base.js
https://github.com/nodejs/undici/blob/v6.6.2/lib/dispatcher.js

After searching, I found that connect function is assigned to Dispatcher on index.js.

https://github.com/nodejs/undici/blob/v6.6.2/index.js#L24
https://github.com/nodejs/undici/blob/v6.6.2/lib/api/index.js
https://github.com/nodejs/undici/blob/v6.6.2/lib/api/api-connect.js


I hope the above report will help to resolve this issue.

corepack is part of nodejs package distributed by NodeSource.
I'm affected by the same issue after upgrade to nodejs=20.12.0-1nodesource1 package.
To downgrade:

sudo apt install nodejs=20.11.1-1nodesource1

Yes, I am also using nodejs distributed by NodeSource.
I appended the version information to my report.

Probably import ProxyAgent from undici/index.js instead of undici/lib/proxy-agent.js, e.g. as below:

diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts
index e995d1e..b856099 100644
--- a/sources/httpUtils.ts
+++ b/sources/httpUtils.ts
@@ -100,11 +100,7 @@ async function getProxyAgent(input: string | URL) {
 
   if (!proxy) return undefined;
 
-  // Doing a deep import here since undici isn't tree-shakeable
-  const {default: ProxyAgent} = (await import(
-    // @ts-expect-error No types for this specific file
-    `undici/lib/proxy-agent.js`
-  )) as { default: typeof import('undici').ProxyAgent };
+  const {ProxyAgent} = await import(`undici`);
 
   return new ProxyAgent(proxy);
 }

Probably import ProxyAgent from undici/index.js instead of undici/lib/proxy-agent.js

AFAICT it should not have any effect – except making the bundle size bigger. But it's certainly something we can try, we'd just need a way to reproduce the issue first so we can validate if that has any effect.

@aduh95 I'm trying to reproduce. Is it the way to reproduce that you expected?

  1. Clone corepack repository.
  2. yarn install
  3. yarn add proxy --dev
  4. yarn build
  5. Write a code named tests/issue444.test.ts, described below.
  6. yarn test -- issue444
import {afterAll, beforeAll, describe, expect, it} from '@jest/globals';
import process from 'node:process';

import { createProxy } from 'proxy'; // Need `yarn add proxy --dev`.
import type { ProxyServer } from 'proxy';

import { fetchAsJson } from '../sources/httpUtils';

describe(`reproduce issue #444`, () => {

  let proxy: ProxyServer;

  beforeAll((done) => {
    proxy = createProxy();
    proxy.listen(done);
  })

  afterAll((done) => {
    proxy.close(done);
  })

  it(`without proxy settings`, async () => {
    await expect(fetchAsJson(`https://registry.npmjs.org/yarn`)).resolves.toEqual(expect.anything());
  });

  it(`with proxy settings`, async () => {
    process.env.HTTPS_PROXY = `http://localhost:${proxy.address().port}`;

    await expect(fetchAsJson(`https://registry.npmjs.org/yarn`)).resolves.toEqual(expect.anything());
    // TODO: Test fails.
    // Received promise rejected instead of resolved
    // Rejected to value: [Error: Error when performing the request to https://registry.npmjs.org/yarn; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting]
  });

});

Looks like the same reason as here currently in discussion. So my issue is closed.

Duplicate of #417