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

[🐛 Bug]: ERROR Could not resolve "async_hooks" #908

Closed
1 task
jellohouse opened this issue Nov 27, 2024 · 58 comments · Fixed by #909
Closed
1 task

[🐛 Bug]: ERROR Could not resolve "async_hooks" #908

jellohouse opened this issue Nov 27, 2024 · 58 comments · Fixed by #909
Labels
bug Something isn't working

Comments

@jellohouse
Copy link

next-on-pages environment related information

System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
CPU: (10) arm64 Apple M1 Max
Memory: 32 GB
Shell: /bin/zsh
Package Manager Used: npm (10.2.4)

Relevant Packages:
@cloudflare/next-on-pages: 1.13.5
vercel: N/A
next: 14.2.18

Description

It just started happening out of the blue. Both when i try to build locally or when I git push and Cloudflare does the build for production.

Even on Cloudflare pages if i try to re-deploy an old commit version that had previously deployed successfully, it also fails.

Not sure exactly what the issue is...

This is what i see in the terminal logs, and i see the error comes after the next build has completed:

[next js build stuff completes succesfully]
...
▲  Build Completed in .vercel/output [33s]
⚡️ Completed `npx vercel build`.
✘ [ERROR] Could not resolve "async_hooks"

    <stdin>:596:680:
      596 │ ...umerable:!0})),e);_(exports,{default:()=>N});var R=A(require("async_hooks")),S="@next/request-context",f=Symbol.for(S),C=Symbol.for("i...
          ╵                                                                 ~~~~~~~~~~~~~

  The package "async_hooks" wasn't found on the file system but is built into node. Are you trying
  to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1575
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 1 error:
<stdin>:596:680: ERROR: Could not resolve "async_hooks"
    at failureErrorWithLog (/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1575:15)
    at /Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1033:28
    at runOnEndCallbacks (/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1447:61)
    at buildResponseToResult (/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1031:7)
    at /Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1143:14
    at responseCallbacks.<computed> (/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:680:9)
    at handleIncomingPacket (/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:735:9)
    at Socket.readFromStdout (/Users/myuser/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:656:7)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 680,
        file: '<stdin>',
        length: 13,
        line: 596,
        lineText: '    const fn = (function(module,exports) {var b=Object.create;var p=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var h=e=>p(e,"__esModule",{value:!0});var _=(e,n)=>{h(e);for(var t in n)p(e,t,{get:n[t],enumerable:!0})},U=(e,n,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of T(n))!L.call(e,i)&&i!=="default"&&p(e,i,{get:()=>n[i],enumerable:!(t=w(n,i))||t.enumerable});return e},A=e=>U(h(p(e!=null?b(P(e)):{},"default",e&&e.__esModule&&"default"in e?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);_(exports,{default:()=>N});var R=A(require("async_hooks")),S="@next/request-context",f=Symbol.for(S),C=Symbol.for("internal.storage");function O(){let e=globalThis;if(!e[f]){let n=new R.AsyncLocalStorage,t={get:()=>n.getStore(),[C]:n};e[f]=t}return e[f]}var q=O();function m(e,n){return q[C].run(e,n)}function y(e){let n={};return e&&e.forEach((t,i)=>{n[i]=t,i.toLowerCase()==="set-cookie"&&(n[i]=M(t))}),n}function M(e){let n=[],t=0,i,a,g,o,r;function x(){for(;t<e.length&&/\\s/.test(e.charAt(t));)t+=1;return t<e.length}function s(){return a=e.charAt(t),a!=="="&&a!==";"&&a!==","}for(;t<e.length;){for(i=t,r=!1;x();)if(a=e.charAt(t),a===","){for(g=t,t+=1,x(),o=t;t<e.length&&s();)t+=1;t<e.length&&e.charAt(t)==="="?(r=!0,t=o,n.push(e.substring(i,g)),i=t):t=g+1}else t+=1;(!r||t>=e.length)&&n.push(e.substring(i,e.length))}return n}function N(e){let n=e.staticRoutes.map(i=>({regexp:new RegExp(i.namedRegex),page:i.page})),t=e.dynamicRoutes?.map(i=>({regexp:new RegExp(i.namedRegex),page:i.page}))||[];return async function(a,g){let o=new URL(a.url).pathname,r={};if(e.nextConfig?.basePath&&o.startsWith(e.nextConfig.basePath)&&(o=o.replace(e.nextConfig.basePath,"")||"/"),e.nextConfig?.i18n)for(let s of e.nextConfig.i18n.locales){let u=new RegExp(`^/${s}($|/)`,"i");if(o.match(u)){o=o.replace(u,"/")||"/";break}}for(let s of n)if(s.regexp.exec(o)){r.name=s.page;break}if(!r.name){let s=E(o);for(let u of t||[]){if(s&&!E(u.page))continue;let d=u.regexp.exec(o);if(d){r={name:u.page,params:d.groups};break}}}let x=await m({waitUntil:g.waitUntil},()=>globalThis._ENTRIES[`middleware_${e.name}`].default.call({},{request:{url:a.url,method:a.method,headers:y(a.headers),ip:c(a.headers,l.Ip),geo:{city:c(a.headers,l.City,!0),country:c(a.headers,l.Country,!0),latitude:c(a.headers,l.Latitude),longitude:c(a.headers,l.Longitude),region:c(a.headers,l.Region,!0)},nextConfig:e.nextConfig,page:r,body:a.body}}));return x.waitUntil&&g.waitUntil(x.waitUntil),x.response}}function c(e,n,t=!1){let i=e.get(n)||void 0;return t&&i?decodeURIComponent(i):i}function E(e){return e==="/api"||e.startsWith("/api/")}var l;(function(o){o.City="x-vercel-ip-city",o.Country="x-vercel-ip-country",o.Ip="x-real-ip",o.Latitude="x-vercel-ip-latitude",o.Longitude="x-vercel-ip-longitude",o.Region="x-vercel-ip-country-region"})(l||(l={}));',
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: `The package "async_hooks" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.`
        }
      ],
      pluginName: '',
      text: 'Could not resolve "async_hooks"'
    }
  ],
  warnings: []
}

Node.js v21.6.1

Reproduction

No response

Pages Deployment Method

Pages CI (GitHub/GitLab integration)

Pages Deployment ID

No response

Additional Information

No response

Would you like to help?

  • Would you like to help fixing this bug?
@jellohouse jellohouse added the bug Something isn't working label Nov 27, 2024
@chrisp-code
Copy link

chrisp-code commented Nov 27, 2024

I am encountering the same (only difference is Node.js v20.10.0 and [email protected]). I can't deploy on Cloudflare.

@hitsthings
Copy link

hitsthings commented Nov 27, 2024

Seeing it on Node.js v20.18.1 also with these versions:

⚡️ @cloudflare/next-on-pages CLI v.1.13.5
⚡️ Detected Package Manager: npm (10.8.2)
▲  Vercel CLI 37.5.3
next: "14.2.18",

Interestingly, while it is failing in GitHub Actions, it works for me locally on OSX with the same version for all of these dependencies). Maybe if I do an npm install that will change.

@hitsthings
Copy link

hitsthings commented Nov 27, 2024

Deleting .vercel caused it to start failing locally. Unfortunately I didn't check the node_modules in there before deleting.

The dependency versions in .vercel/builders/package.json afterward are "@vercel/build-utils":"^8.6.0","@vercel/next":"^4.4.0"}.

I am not sure, but it seems like I was on @vercel/build-utils 8.4.3 before that - 8.4.3 is what is in my package's own node-modules folder.

@vercel/[email protected] and @vercel/[email protected] were published 5 hours ago.
https://github.com/vercel/vercel/commits/main/packages/build-utils - change since 8.5.0 seems benign?

https://github.com/vercel/vercel/commits/main/packages/next does NOT seem benign. async_hooks was externalized in a build if I am reading correctly. vercel/vercel@ef2a4aa#diff-e56444ce606e001cf55598bba6f31f9368a3389b86ee1496a860fb339c5dc9d2R22

@savokiss
Copy link

Same issue with async_hooks error, the same commit was successful a few days ago, but retrying to deploy it today failed.

@sampotts
Copy link

sampotts commented Nov 27, 2024

The workaround we found worked was rolling back vercel and pinning and overriding it to 39.1.1. Assuming you're using npm, you can use this:

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}

You'll also have wipe out any references to vercel from package-lock.json and run npx npkill to clear out node_modules of old versions.

@andida
Copy link

andida commented Nov 27, 2024

I also face the issue.

@jellohouse
Copy link
Author

The workaround we found worked was rolling back vercel and pinning and overriding it to 39.1.1. Assuming you're using npm, you can use this:

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}

You'll also have wipe out any references to vercel from package-lock.json and run npx npkill to clear out node_modules of old versions.

This seems to fix it for now thanks @sampotts

@c0da523
Copy link

c0da523 commented Nov 27, 2024

Same problem I meet, almost no content has been changed. Yesterday it compiled OK, but today it reported an error.

✘ [ERROR] Could not resolve "async_hooks"

    <stdin>:328:680:
      328 │ ...enumerable:!0}:{value:e,enumerable:!0})),e);_(exports,{default:()=>N});var R=A(require("async_hooks")),S="@next/request-context",f=Symbol.for(S),C=Symbol.for("internal.storage");function...
          ╵                                                                                           ~~~~~~~~~~~~~

  The package "async_hooks" wasn't found on the file system but is built into node. Are you trying
  to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1575
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 1 error:
<stdin>:328:680: ERROR: Could not resolve "async_hooks"
    at failureErrorWithLog (/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1575:15)
    at /Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1033:28
    at runOnEndCallbacks (/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1447:61)
    at buildResponseToResult (/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1031:7)
    at /Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1143:14
    at responseCallbacks.<computed> (/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:680:9)
    at handleIncomingPacket (/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:735:9)
    at Socket.readFromStdout (/Users/coda/Workspace/p-wkspace-website/polyhedra-network-new/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:656:7)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 680,
        file: '<stdin>',
        length: 13,
        line: 328,
        lineText: '    const fn = (function(module,exports) {var b=Object.create;var p=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var h=e=>p(e,"__esModule",{value:!0});var _=(e,n)=>{h(e);for(var t in n)p(e,t,{get:n[t],enumerable:!0})},U=(e,n,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of T(n))!L.call(e,i)&&i!=="default"&&p(e,i,{get:()=>n[i],enumerable:!(t=w(n,i))||t.enumerable});return e},A=e=>U(h(p(e!=null?b(P(e)):{},"default",e&&e.__esModule&&"default"in e?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);_(exports,{default:()=>N});var R=A(require("async_hooks")),S="@next/request-context",f=Symbol.for(S),C=Symbol.for("internal.storage");function O(){let e=globalThis;if(!e[f]){let n=new R.AsyncLocalStorage,t={get:()=>n.getStore(),[C]:n};e[f]=t}return e[f]}var q=O();function m(e,n){return q[C].run(e,n)}function y(e){let n={};return e&&e.forEach((t,i)=>{n[i]=t,i.toLowerCase()==="set-cookie"&&(n[i]=M(t))}),n}function M(e){let n=[],t=0,i,a,g,o,r;function x(){for(;t<e.length&&/\\s/.test(e.charAt(t));)t+=1;return t<e.length}function s(){return a=e.charAt(t),a!=="="&&a!==";"&&a!==","}for(;t<e.length;){for(i=t,r=!1;x();)if(a=e.charAt(t),a===","){for(g=t,t+=1,x(),o=t;t<e.length&&s();)t+=1;t<e.length&&e.charAt(t)==="="?(r=!0,t=o,n.push(e.substring(i,g)),i=t):t=g+1}else t+=1;(!r||t>=e.length)&&n.push(e.substring(i,e.length))}return n}function N(e){let n=e.staticRoutes.map(i=>({regexp:new RegExp(i.namedRegex),page:i.page})),t=e.dynamicRoutes?.map(i=>({regexp:new RegExp(i.namedRegex),page:i.page}))||[];return async function(a,g){let o=new URL(a.url).pathname,r={};if(e.nextConfig?.basePath&&o.startsWith(e.nextConfig.basePath)&&(o=o.replace(e.nextConfig.basePath,"")||"/"),e.nextConfig?.i18n)for(let s of e.nextConfig.i18n.locales){let u=new RegExp(`^/${s}($|/)`,"i");if(o.match(u)){o=o.replace(u,"/")||"/";break}}for(let s of n)if(s.regexp.exec(o)){r.name=s.page;break}if(!r.name){let s=E(o);for(let u of t||[]){if(s&&!E(u.page))continue;let d=u.regexp.exec(o);if(d){r={name:u.page,params:d.groups};break}}}let x=await m({waitUntil:g.waitUntil},()=>globalThis._ENTRIES[`middleware_${e.name}`].default.call({},{request:{url:a.url,method:a.method,headers:y(a.headers),ip:c(a.headers,l.Ip),geo:{city:c(a.headers,l.City,!0),country:c(a.headers,l.Country,!0),latitude:c(a.headers,l.Latitude),longitude:c(a.headers,l.Longitude),region:c(a.headers,l.Region,!0)},nextConfig:e.nextConfig,page:r,body:a.body}}));return x.waitUntil&&g.waitUntil(x.waitUntil),x.response}}function c(e,n,t=!1){let i=e.get(n)||void 0;return t&&i?decodeURIComponent(i):i}function E(e){return e==="/api"||e.startsWith("/api/")}var l;(function(o){o.City="x-vercel-ip-city",o.Country="x-vercel-ip-country",o.Ip="x-real-ip",o.Latitude="x-vercel-ip-latitude",o.Longitude="x-vercel-ip-longitude",o.Region="x-vercel-ip-country-region"})(l||(l={}));',
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: `The package "async_hooks" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.`
        }
      ],
      pluginName: '',
      text: 'Could not resolve "async_hooks"'
    }
  ],
  warnings: []
}

Node.js v20.16.0

@kaiono-g
Copy link

This solution didn't work for me...
I still have same error.

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}

@danieldando
Copy link

Add "vercel": "39.1.1" under devDependencies in your package.json. This worked perfectly for me!

@ayushkarn32
Copy link

ayushkarn32 commented Nov 27, 2024

adding vercel in devdependencies in package.json worked for me

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
}

@hitsthings
Copy link

hitsthings commented Nov 27, 2024

@kaiono-g you might have to: run npm install and delete the .vercel folder.
When my .vercel folder was recreated it no longer had the builders subdirectory at all and started pulling from my package's node_modules instead.

@james-elicx
Copy link
Contributor

image

:(

I believe we wouldn't have this problem if they prefixed the import with node:

@savokiss
Copy link

savokiss commented Nov 27, 2024

If you are using pnpm, try this:

Remember to update the lockfile and clear the build cache.

@Rao-Abrar-Ahmad
Copy link

Rao-Abrar-Ahmad commented Nov 27, 2024

The workaround we found worked was rolling back vercel and pinning and overriding it to 39.1.1. Assuming you're using npm, you can use this:

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}

You'll also have wipe out any references to vercel from package-lock.json and run npx npkill to clear out node_modules of old versions.

This fix works for me. I got the same issue when deploying on Cloudflare pages

But why do we need the Vercel package when deploying it to Cloudflare?

@Gergling
Copy link

I've had a similar experience. Yesterday deployments were fine, today they're producing this error. The main differences I observe are that I'm using NX and Yarn, and my app-level package.json file is being overwritten.

I modified /package.json and /apps/next-js/package.json to have the version resolution (as above, but yarn-flavoured):

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "resolutions": {
    "vercel": "39.1.1"
  }
}

Here are the steps I took:

  1. I deleted /node_modules, /apps/next-js/node_modules, /apps/next-js/.vercel, /apps/next-js/.next for good measure, and every instance of vercel from /yarn.lock and /apps/next-js/yarn.lock. Then I ran yarn at the root level.
  2. I observed that in /yarn.lock my version had changed from ^37.0.0 to 39.1.1. IMO this is expected, but do correct me if I'm wrong.
  3. Then I ran the next-on-pages command.

I observed:

  • The same error as reported.
  • The /apps/next-js/package.json file had changed to:
{
  "devDependencies": {
    "vercel": "^39.1.2"
  },
}
  • There is an entry near the beginning of the command log saying vercel dev dependency missing, installing vercel as a dev dependency with 'yarn add vercel -D'....
  • /apps/next-js/yarn.lock had an entry for both 39.1.1 and ^39.1.2.

A colleague with the same setup was able to reproduce the same issue with the package.json file.

Is there something I need to be doing to avoid it running that superflous dev dependency installation? If so, I might actually be able to implement that other downgrade properly.

Have attached a screenshot of my log output where it's installing the dependency.
Screenshot 2024-11-27 at 12 21 57

@tewshi0
Copy link

tewshi0 commented Nov 27, 2024

Even after adding "vercel": "39.1.1" to devDependencies the build on cloudflare still fails

19:05:55.565 | ⚡️ @cloudflare/next-on-pages CLI v.1.13.5
-- | --
19:05:55.906 | ⚡️ Detected Package Manager: npm (9.7.2)
19:05:55.906 | ⚡️ Preparing project...
19:05:55.909 | ⚡️ Project is ready
19:05:55.909 | ⚡️ Building project...
19:05:56.848 | ▲  npm WARN exec The following package was not found and will be installed: [email protected]
19:06:00.753 | ▲  npm
                        ...
                        ...
19:06:56.472 | ▲  Traced Next.js server files in: 239.758ms
-- | --
19:06:56.539 | ▲  Created all serverless functions in: 66.363ms
19:06:58.521 | ▲  Collected static files (public/, static/, .next/static): 8.284ms
19:06:58.762 | ▲  Build Completed in .vercel/output [57s]
19:06:58.816 | ⚡️ Completed `npx vercel build`.
19:07:02.213 | ✘ [ERROR] Could not resolve "async_hooks"
19:07:02.213 |  
19:07:02.214 | <stdin>:327:680:
19:07:02.214 | 327 │ ...:()=>N});var R=A(require("async_hooks")),S="@next/request-cont...
19:07:02.214 | ╵                             ~~~~~~~~~~~~~
                        ...
                        ...
19:07:02.229 | warnings: []
-- | --
19:07:02.230 | }
19:07:02.230 |  
19:07:02.230 | Node.js v20.4.0
19:07:02.260 | Failed: Error while executing user command. Exited with error code: 1
19:07:02.272 | Failed: build command exited with code: 1
19:07:03.173 | Failed: error occurred while running build command

Works well in my local build and github actions but for some weird reason during cloudflare build it still uses [email protected]

@mjp0
Copy link

mjp0 commented Nov 27, 2024

I'm using pnpm and fixed the vercel version to 39.1.1. I can also see from CF's build log that it's using 39.1.1, yet it still fails with [ERROR] Could not resolve "async_hooks".

CF deployment log:

14:41:22.183 | ▲  devDependencies:
-- | --
14:41:22.183 | ▲  + @types/node 22.10.0
14:41:22.184 | ▲  + @types/react 18.3.12
14:41:22.184 | ▲  + @types/react-dom 18.3.1
14:41:22.185 | ▲  + @types/rss 0.0.32
14:41:22.185 | ▲  + eslint 9.15.0
14:41:22.185 | ▲  + eslint-config-next 14.2.5
14:41:22.185 | ▲  + postcss 8.4.49
14:41:22.185 | ▲  + tailwindcss 3.4.15
14:41:22.185 | ▲  + typescript 5.7.2
14:41:22.186 | ▲  + vercel 39.1.1
14:41:22.300 | ▲  Done in 3.2s

Everything compiles fine locally with pnpm dlx @cloudflare/next-on-pages, it's only the CF deployment that fails.

edit
I was able to deploy the site from local with wrangler pages deploy, so if you can build it locally, you can at least get it deployed while we wait for this to be sorted out.

@aluchaninov
Copy link

I'm using pnpm and fixed the vercel version to 39.1.1. I can also see from CF's build log that it's using 39.1.1, yet it still fails with [ERROR] Could not resolve "async_hooks".

CF deployment log:

14:41:22.183 | ▲  devDependencies:
-- | --
14:41:22.183 | ▲  + @types/node 22.10.0
14:41:22.184 | ▲  + @types/react 18.3.12
14:41:22.184 | ▲  + @types/react-dom 18.3.1
14:41:22.185 | ▲  + @types/rss 0.0.32
14:41:22.185 | ▲  + eslint 9.15.0
14:41:22.185 | ▲  + eslint-config-next 14.2.5
14:41:22.185 | ▲  + postcss 8.4.49
14:41:22.185 | ▲  + tailwindcss 3.4.15
14:41:22.185 | ▲  + typescript 5.7.2
14:41:22.186 | ▲  + vercel 39.1.1
14:41:22.300 | ▲  Done in 3.2s

Everything compiles fine locally with pnpm dlx @cloudflare/next-on-pages, it's only the CF deployment that fails.

edit I was able to deploy the site from local with wrangler pages deploy, so if you can build it locally, you can at least get it deployed while we wait for this to be sorted out.

we are using github actions and Cloudflare (yes, two separate projects with different CI/CD) and experiencing same issue - locally proposed solution with fixing vercel package version to 39.. did the trick but in both CI/CDs problem still exists

@nicubarbaros
Copy link

Same here. Next + Cloudflare

This worked

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}
``

@chrisp-code
Copy link

I suspect the cause to be this:

const execStr = await pm.getRunExec('vercel', {
    args: ['build'],
    download: 'prefer-if-needed',
});

Which will use the latest version of vercel by default.

For pnpm users: I was calling pnpm dlx @cloudflare/next-on-pages for my cloudflare build command to avoid installing dev deps for the production build.

The workarounds using overrides/pnpm.overrides didn't work alone. I had to move @cloudflare/next-on-pages and vercel from devDependencies to dependencies and use pnpm exec next-on-pages for my build command to resolve the issue.

@vinayraghu1
Copy link

@IgorMinar Can we pin the Vercel dependency version to a known number so this doesn't happen every time Vercel cuts a release?

@doubleppereira
Copy link

using yarn and trying to pin the version like this:

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "resolutions": {
    "vercel": "39.1.1"
  }
}

but it's just not working. I keep getting this error

@davyvong
Copy link

I tried to pin the version to 39.1.1 and it did not work as well.

I was able to resolve the build issue by following @chrisp-code's comment and moving "vercel": "39.1.1" from "devDependencies" to "dependencies". Make sure to update your lock file.

@AbdulBima
Copy link

The workaround we found worked was rolling back vercel and pinning and overriding it to 39.1.1. Assuming you're using npm, you can use this:

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}

You'll also have wipe out any references to vercel from package-lock.json and run npx npkill to clear out node_modules of old versions.

Don't forget to npm i after

@harveyjing
Copy link

So? Closed it without fixing? Pining the version just temporarily work around.

@james-elicx
Copy link
Contributor

james-elicx commented Nov 28, 2024

So? Closed it without fixing? Pining the version just temporarily work around.

It has been fixed in the new release - v1.13.6

Note

Please bump your Pages project's compatibility date setting to the most recent one for both Preview and Production environments so that you can use the new version of nodejs_compat.

image

@harveyjing
Copy link

harveyjing commented Nov 28, 2024

So? Closed it without fixing? Pining the version just temporarily work around.

It has been fixed in the new release - v1.13.6

Yes, I have tried. It works. Thank you!

@jellohouse
Copy link
Author

I tried building with the new release v1.13.6

The build works (both locally and in Cloudflare). When i do npm run pages:dev the site works well locally.

But in production loading the website page gives an error 500 and i see this in the Cloudflare logs:

"logs": [
    {
      "message": [
        "Error: No such module \"__next-on-pages-dist__/functions/async_hooks\".\n  imported from \"__next-on-pages-dist__/functions/middleware.func.js\""
      ],
      "level": "error",
      "timestamp": 1732807745843
    }
  ],

@grantv
Copy link

grantv commented Nov 28, 2024

image

:(

I believe we wouldn't have this problem if they prefixed the import with node:

So FYI even with the fix I started to get the following with NextAuth:

TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.

at K.waitUntil
(file:///***/.vercel/output/static/_worker.js/__next-on-pages-dist__/functions/api/auth/[...nextauth].func.js:2:828)

Implementing the vercel version into devDependencies and even with the latest next-on-pages and I am working again at least - but something has definitely broken between Cloudflare and Vercel ...

@james-elicx
Copy link
Contributor

james-elicx commented Nov 28, 2024

I tried building with the new release v1.13.6

The build works (both locally and in Cloudflare). When i do npm run pages:dev the site works well locally.

But in production loading the website page gives an error 500 and i see this in the Cloudflare logs:

"logs": [
    {
      "message": [
        "Error: No such module \"__next-on-pages-dist__/functions/async_hooks\".\n  imported from \"__next-on-pages-dist__/functions/middleware.func.js\""
      ],
      "level": "error",
      "timestamp": 1732807745843
    }
  ],

Are you using a compability date after Sept 23rd 2024? Non-node prefixed imports require nodejs_compat_v2, which was merged into nodejs_compat on compatibility dates after Sept 23rd.

image

Note

Please bump your Pages project's compatibility date setting to the most recent one for both Preview and Production environments.

image

@bradley
Copy link

bradley commented Nov 28, 2024

@bradley's solution worked for us! you are a genius!

thank you

@lcnogueira
Copy link

lcnogueira commented Nov 28, 2024

Thanks so much for the detailed instructions, @james-elicx . Updating the package fixed the build and bumping the compatibility date fixed the prod issue 🤝

I tried building with the new release v1.13.6
The build works (both locally and in Cloudflare). When i do npm run pages:dev the site works well locally.
But in production loading the website page gives an error 500 and i see this in the Cloudflare logs:

"logs": [
    {
      "message": [
        "Error: No such module \"__next-on-pages-dist__/functions/async_hooks\".\n  imported from \"__next-on-pages-dist__/functions/middleware.func.js\""
      ],
      "level": "error",
      "timestamp": 1732807745843
    }
  ],

Are you using a compability date after Sept 23rd 2024? Non-node prefixed imports require nodejs_compat_v2, which was merged into nodejs_compat on compatibility dates after Sept 23rd.

image Note

Please bump your Pages project's compatibility date setting to the most recent one for both Preview and Production environments.

image

@jellohouse
Copy link
Author

That worked for me too ^^^ thanks @james-elicx

@beardsleym
Copy link

Thanks, I think i just had to clear the build cache and now they are working again.

@nicubarbaros
Copy link

worked with the new compatibility date

lyc8503 added a commit to lyc8503/onedrive-cf-index-ng that referenced this issue Nov 29, 2024
lyc8503 added a commit to lyc8503/onedrive-cf-index-ng that referenced this issue Nov 29, 2024
@koribot
Copy link

koribot commented Nov 30, 2024

It worked on the latest 1.13.6 thanks

@ysalitrynskyi
Copy link

ysalitrynskyi commented Dec 5, 2024

Still having this error with 1.13.6

UPD: Fixed by adding this to wrangler.toml:

compatibility_date = "2024-09-23"

@whateverneveranywhere
Copy link

whateverneveranywhere commented Dec 5, 2024

getting this #908 (comment) issue using:

compatibility_date = "2024-11-11"
or
compatibility_date = "2024-09-23"

with

  "devDependencies": {
    "vercel": "39.1.1",
    }
  "overrides": {
    "vercel": "$vercel"
  },

bootleq added a commit to bootleq/feeders that referenced this issue Dec 15, 2024
See next-on-pages issue 908
cloudflare/next-on-pages#908 (comment)

Available dates can be checked in cloudflare dashboard, also need to set
it manually for preview and production environments.
davidar added a commit to compute-toys/compute.toys that referenced this issue Dec 17, 2024
kollersi-me added a commit to kollersi-me/kol-one that referenced this issue Dec 24, 2024
kollersi-me added a commit to kollersi-me/kol-one that referenced this issue Dec 24, 2024
@yang2yang
Copy link

image
please remember to update the document,I spent one or two hours to find this solution.So sad.

@Puliczek
Copy link

Final Solution:

npm i @cloudflare/next-on-pages

and

compatibility_date = "2024-09-23"

@w3vish
Copy link

w3vish commented Dec 31, 2024

The workaround we found worked was rolling back vercel and pinning and overriding it to 39.1.1. Assuming you're using npm, you can use this:

{ 
  "devDependencies": {
    "vercel": "39.1.1"
  },
  "overrides": {
    "vercel": "$vercel"
  }
}

You'll also have wipe out any references to vercel from package-lock.json and run npx npkill to clear out node_modules of old versions.

thank you so much bro it works for me, thanks again

@YalDan
Copy link

YalDan commented Jan 6, 2025

Final Solution:

npm i @cloudflare/next-on-pages

and

compatibility_date = "2024-09-23"

Damn I spent a whole day trying to make it work and this did the trick. Thank you so much!

@verdverm
Copy link

I hit this issue as well, my fix did not require changing package versions for anything from npm

In the end, all I had to do was update the compatibility_date = "2024-09-23" which is newer than you will find in the cloudflare + nextjs docs in the wrangler example

@GrahamQuan
Copy link

this works for me, i use next-intl as i18n middleware

{
  "name": "...",
  "compatibility_date": "2024-09-23", // update the date
  "compatibility_flags": ["nodejs_compat"],
  "pages_build_output_dir": ".vercel/output/static"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.