Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not support proxy-agent #47039

Open
1 task done
coderzzp opened this issue Mar 12, 2023 · 7 comments
Open
1 task done

not support proxy-agent #47039

coderzzp opened this issue Mar 12, 2023 · 7 comments
Labels
bug Issue was opened via the bug report template.

Comments

@coderzzp
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 17.7.0: Fri Oct 30 13:34:27 PDT 2020; root:xnu-4570.71.82.8~1/RELEASE_X86_64
    Binaries:
      Node: 16.19.1
      npm: 8.19.3
      Yarn: 1.3.2
      pnpm: 7.29.1
    Relevant packages:
      next: 13.2.3
      eslint-config-next: 13.2.3
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

no

To Reproduce

  const res = await fetch('https://api.openai.com/v1/chat/completions', {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.OPENAI_API_KEY ?? ''}`,
    },
    agent: new ProxyAgent('http://127.0.0.1:7890'),
    method: 'POST',
    body: JSON.stringify(payload),
  })

Describe the Bug

proxy failed and fetch failed

error - Error: The edge runtime does not support Node.js 'events' module.

Expected Behavior

ProxyAgent can proxy correctly rather than report error

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@coderzzp coderzzp added the bug Issue was opened via the bug report template. label Mar 12, 2023
@liudhzhyym
Copy link

any solutions?

@coderzzp
Copy link
Author

any solutions?

not yet

@wenerme
Copy link

wenerme commented Mar 31, 2023

NextJS support proxy just like nodejs https://github.com/Yidadaa/ChatGPT-Next-Web/pull/295/files

I extract the core part to here https://github.com/wenerme/wode/blob/main/packages/utils/src/servers/createFetchWithProxyByNextUndici.ts

@jernkuan
Copy link

an alternative is to use global-agent and cross-fetch/polyfill

for dev env setup
run the following
node -r global-agent/boot-strap -r cross-fetch/polyfill --no-experiemental-fetch node_modules/next/dist/bin/next dev

Remember to setup the GLOBAL_AGENT_HTTP_PROXY etc.

@aksjer
Copy link

aksjer commented Dec 21, 2023

It does not work with the next version 13 or 14 which uses undici

@padupuy
Copy link

padupuy commented Jun 25, 2024

Thanks to this comment #66373 (comment) I successfully setup a global proxy agent with no custom server inside the layout.ts

import { ProxyAgent, setGlobalDispatcher } from "undici";

if (process.env.HTTP_PROXY) {
  const proxyAgent = new ProxyAgent(process.env.HTTP_PROXY);
  setGlobalDispatcher(proxyAgent);
}

export default async function Layout({
  children,
}: {
  children: React.ReactNode;
}) {
  
  return (
    <html >
      <body>
          {children}
      </body>
    </html>
  );
}

@thescientist13
Copy link

I was also able to leverage undici + setGlobalDispatcher in a Next 14 app using a custom server.js and it seems to be working well

const { ProxyAgent, setGlobalDispatcher } = require('undici');

// ...

const { PROXY_HOST, PROXY_PORT } = process.env;
setGlobalDispatcher(new ProxyAgent(`http://${PROXY_HOST}:${PROXY_PORT}`));

const app = next({ /* ... */ });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

7 participants