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

Closed
1 task done
coderzzp opened this issue Mar 12, 2023 · 10 comments
Closed
1 task done

not support proxy-agent #47039

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

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({ /* ... */ });

@abalmus
Copy link

abalmus commented Sep 19, 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>
  );
}

Good one, I also looking at the instrumentationHook, and use instrumentation.js to set the proxy agent, instead of layout. https://nextjs.org/docs/app/api-reference/next-config-js/instrumentationHook

@ztanner
Copy link
Member

ztanner commented Sep 20, 2024

Closing as there appear to be working solutions provided above and the original issue does not provide a valid reproduction (and may be out of date as the fetch builtin is Undici which leverages dispatchers). Please feel free to create a new issue following the bug report template if you have a valid reproduction of a bug.

@ztanner ztanner closed this as completed Sep 20, 2024
Copy link
Contributor

github-actions bot commented Oct 4, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Oct 4, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

No branches or pull requests

9 participants