cloudflare / next-on-pages

CLI to build and develop Next.js apps for Cloudflare Pages
https://www.npmjs.com/package/@cloudflare/next-on-pages
MIT License
1.28k stars 125 forks source link

[πŸ› Bug]: Nextjs v13.3.1 fresh install worker threw exception #199

Closed hjaber closed 1 year ago

hjaber commented 1 year ago

Cli version

0.10.0

next-on-pages environment related information

System:
        Platform: darwin
        Arch: arm64
        Version: Darwin Kernel Version 22.4.0: Mon Mar  6 21:01:02 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8112
        CPU: (8) arm64 Apple M2
        Memory: 24 GB
        Shell: /bin/zsh
Binaries:
        Node: 18.12.1
        Yarn: N/A
        npm: 9.6.5
        pnpm: N/A
Package Manager Used: npm
Relevant Packages:
        @cloudflare/next-on-pages: 0.10.0
        vercel: N/A
        next: 13.3.1

Description

I see that issue #186 is already merged but I'm having this exception thrown in prod

Error 1101 Worker threw exception

{
  "outcome": "exception",
  "scriptName": "pages-worker--1029254-production",
  "exceptions": [
    {
      "name": "Error",
      "message": "Dynamic require of \"node:buffer\" is not supported",
      "timestamp": 1682397982459
    }
  ],
  "logs": [],
  "eventTimestamp": 1682397982459,
  "event": {
    "request": {
      "url": "https://08541378.mech.pages.dev/",
      "method": "GET",
      "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
        "accept-encoding": "gzip",
        "accept-language": "en-US,en;q=0.8",
        "cache-control": "max-age=0",
        "cf-connecting-ip": "107.199.200.137",
        "cf-ipcountry": "US",
        "cf-ray": "7bd3f11e5f0ecfa8",
        "cf-visitor": "{\"scheme\":\"https\"}",
        "connection": "Keep-Alive",
        "dnt": "1",
        "host": "08541378.mech.pages.dev",
        "priority": "u=0, i",
        "sec-ch-ua": "\"Chromium\";v=\"112\", \"Brave\";v=\"112\", \"Not:A-Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"macOS\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "cross-site",
        "sec-fetch-user": "?1",
        "sec-gpc": "1",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
        "x-forwarded-proto": "https",
        "x-real-ip": "107.199.200.137"
      },
      "cf": {
        "longitude": "-122.44140",
        "httpProtocol": "HTTP/3",
        "tlsCipher": "AEAD-AES128-GCM-SHA256",
        "continent": "NA",
        "asn": 7018,
        "clientAcceptEncoding": "gzip, deflate, br",
        "country": "US",
        "tlsClientAuth": {
          "certIssuerDNLegacy": "",
          "certIssuerSKI": "",
          "certSubjectDNRFC2253": "",
          "certSubjectDNLegacy": "",
          "certFingerprintSHA256": "",
          "certNotBefore": "",
          "certSKI": "",
          "certSerial": "",
          "certIssuerDN": "",
          "certVerified": "NONE",
          "certNotAfter": "",
          "certSubjectDN": "",
          "certPresented": "0",
          "certRevoked": "0",
          "certIssuerSerial": "",
          "certIssuerDNRFC2253": "",
          "certFingerprintSHA1": ""
        },
        "tlsExportedAuthenticator": {
          "clientFinished": "1c842cd235a3841d8f6dd4cb2107d630e7c3108f02fc5ac605c9c8d63e65423f",
          "clientHandshake": "24369ebd3f59f1545d30a3792a777dbe6a413036a8f46ba788e3433701812f6b",
          "serverHandshake": "87c529c4b1e801ae4aa0f8c1d636b89a87bfcbea3bf8ae565796207ec10cdd99",
          "serverFinished": "0606d67688e1b65fc915da89032f059eb3c900346437908e25b3f10de8f2a8a8"
        },
        "tlsVersion": "TLSv1.3",
        "city": "San Francisco",
        "timezone": "America/Los_Angeles",
        "requestPriority": "",
        "edgeRequestKeepAliveStatus": 1,
        "postalCode": "94112",
        "colo": "SJC",
        "latitude": "37.72010",
        "region": "California",
        "regionCode": "CA",
        "asOrganization": "AT&T Internet",
        "metroCode": "807",
        "pagesHostName": "08541378.mech.pages.dev"
      }
    },
    "response": {
      "status": 500
    }
  },
  "id": 8
}

Reproduction

https://github.com/hjaber/mech

Additional Information

No response

Would you like to help?

dario-piotrowicz commented 1 year ago

Thanks a lot for the issue @hjaber (and thanks a lot for the reproduction! πŸ™)

It turns out that the solution in the PR had a typo and instead of !== I put there === πŸ˜“

dario-piotrowicz commented 1 year ago

@hjaber if you want you can check the beta release: https://www.npmjs.com/package/@cloudflare/next-on-pages/v/0.0.0-9f5b83c should be all good now πŸ™‚ (and I'll probably do a proper release later today or tomorrow)

hjaber commented 1 year ago

Thank you again @dario-piotrowicz for your prompt fix!

Still getting a build error, should I be manually adding the following to my wrangler.toml file?

Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.

2023-04-25T14:37:56.065216Z Cloning repository...
2023-04-25T14:37:57.115052Z From https://github.com/hjaber/mech
2023-04-25T14:37:57.115708Z  * branch            d74842e13ec16b81561176acf9a3855ffb9980c4 -> FETCH_HEAD
2023-04-25T14:37:57.115955Z 
2023-04-25T14:37:57.171586Z HEAD is now at d74842e demo @cloudflare/next patch
2023-04-25T14:37:57.172255Z 
2023-04-25T14:37:57.280249Z 
2023-04-25T14:37:57.311722Z Success: Finished cloning repository files
2023-04-25T14:37:58.026135Z Installing dependencies
2023-04-25T14:37:58.03377Z  Python version set to 2.7
2023-04-25T14:38:01.26297Z  v18.16.0 is already installed.
2023-04-25T14:38:02.722578Z Now using node v18.16.0 (npm v9.5.1)
2023-04-25T14:38:02.898793Z Started restoring cached build plugins
2023-04-25T14:38:02.910945Z Finished restoring cached build plugins
2023-04-25T14:38:03.302073Z Attempting ruby version 2.7.1, read from environment
2023-04-25T14:38:04.774126Z Using /opt/buildhome/.rvm/gems/ruby-2.7.1
2023-04-25T14:38:05.772344Z Using PHP version 5.6
2023-04-25T14:38:05.86246Z  5.2.5 is already installed.
2023-04-25T14:38:05.879224Z Using Swift version 5.2.5
2023-04-25T14:38:05.880063Z Started restoring cached node modules
2023-04-25T14:38:05.891341Z Finished restoring cached node modules
2023-04-25T14:38:06.752439Z Installing NPM modules using NPM version 9.5.1
2023-04-25T14:38:12.752478Z npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
2023-04-25T14:38:13.46445Z  npm WARN deprecated rollup-plugin-inject@3.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
2023-04-25T14:38:15.580287Z npm WARN deprecated @npmcli/move-file@1.1.2: This functionality has been moved to @npmcli/fs
2023-04-25T14:38:27.949063Z 
2023-04-25T14:38:27.949577Z added 1011 packages, and audited 1012 packages in 20s
2023-04-25T14:38:27.949799Z 
2023-04-25T14:38:27.949942Z 246 packages are looking for funding
2023-04-25T14:38:27.950065Z   run `npm fund` for details
2023-04-25T14:38:27.967631Z 
2023-04-25T14:38:27.968045Z 3 moderate severity vulnerabilities
2023-04-25T14:38:27.968196Z 
2023-04-25T14:38:27.968323Z To address all issues (including breaking changes), run:
2023-04-25T14:38:27.968433Z   npm audit fix --force
2023-04-25T14:38:27.96854Z  
2023-04-25T14:38:27.968744Z Run `npm audit` for details.
2023-04-25T14:38:27.990855Z NPM modules installed
2023-04-25T14:38:28.056883Z Installing Hugo 0.54.0
2023-04-25T14:38:28.718354Z Hugo Static Site Generator v0.54.0-B1A82C61A/extended linux/amd64 BuildDate: 2019-02-01T10:04:38Z
2023-04-25T14:38:28.723188Z Started restoring cached go cache
2023-04-25T14:38:28.738751Z Finished restoring cached go cache
2023-04-25T14:38:28.828169Z go version go1.14.4 linux/amd64
2023-04-25T14:38:28.841865Z go version go1.14.4 linux/amd64
2023-04-25T14:38:28.846622Z Installing missing commands
2023-04-25T14:38:28.846911Z Verify run directory
2023-04-25T14:38:28.847063Z Executing user command: npx @cloudflare/next-on-pages --experimental-minify
2023-04-25T14:38:30.714627Z ⚑️ @cloudflare/next-on-pages CLI v.0.0.0-9f5b83c (beta/canary release)
2023-04-25T14:38:30.717201Z ⚑️ Detected Package Manager: npm
2023-04-25T14:38:30.71748Z  ⚑️ Preparing project...
2023-04-25T14:38:30.720104Z ⚑️ Project is ready
2023-04-25T14:38:30.720342Z ⚑️ Building project...
2023-04-25T14:38:32.100977Z β–²  Vercel CLI 28.20.0
2023-04-25T14:38:32.409812Z β–²  Installing dependencies...
2023-04-25T14:38:34.689366Z β–²  up to date in 2s
2023-04-25T14:38:34.68983Z  β–²  246 packages are looking for funding
2023-04-25T14:38:34.690363Z β–²  run `npm fund` for details
2023-04-25T14:38:34.70505Z  β–²  Detected Next.js version: 13.3.1
2023-04-25T14:38:34.713694Z β–²  Detected `package-lock.json` generated by npm 7+...
2023-04-25T14:38:34.714164Z β–²  Running "npm run build"
2023-04-25T14:38:35.549244Z β–²  > mech@0.1.0 build
2023-04-25T14:38:35.549573Z β–²  > next build
2023-04-25T14:38:36.110686Z β–²  warn  - You have enabled experimental feature (appDir) in next.config.js.
2023-04-25T14:38:36.111411Z β–²  warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
2023-04-25T14:38:36.111686Z β–²  info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback
2023-04-25T14:38:36.146461Z β–²  Attention: Next.js now collects completely anonymous telemetry regarding usage.
2023-04-25T14:38:36.1468Z   β–²  This information is used to shape Next.js' roadmap and prioritize features.
2023-04-25T14:38:36.147022Z β–²  You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
2023-04-25T14:38:36.147317Z β–²  https://nextjs.org/telemetry
2023-04-25T14:38:36.314396Z β–²  info  - Creating an optimized production build...
2023-04-25T14:38:36.519428Z β–²  warn  - You are using an experimental edge runtime, the API might change.
2023-04-25T14:38:48.10533Z  β–²  info  - Compiled successfully
2023-04-25T14:38:48.109136Z β–²  info  - Linting and checking validity of types...
2023-04-25T14:38:50.605343Z β–²  info  - Collecting page data...
2023-04-25T14:39:02.830727Z β–²  info  - Generating static pages (0/3)
2023-04-25T14:39:02.960116Z β–²  info  - Generating static pages (3/3)
2023-04-25T14:39:02.972917Z β–²  info  - Finalizing page optimization...
2023-04-25T14:39:02.978136Z β–²  
2023-04-25T14:39:02.991094Z β–²  Route (app)                                Size     First Load JS
2023-04-25T14:39:02.991341Z β–²  β”Œ ℇ /                                      257 B            76 kB
2023-04-25T14:39:02.991482Z β–²  β”œ ℇ /api/hello                             0 B                0 B
2023-04-25T14:39:02.991603Z β–²  β”” β—‹ /favicon.ico                           0 B                0 B
2023-04-25T14:39:02.991725Z β–²  + First Load JS shared by all              75.8 kB
2023-04-25T14:39:02.991954Z β–²  β”œ chunks/139-f16e2247630ddfd5.js         23.3 kB
2023-04-25T14:39:02.992156Z β–²  β”œ chunks/2443530c-df1bed786c221046.js    50.6 kB
2023-04-25T14:39:02.992372Z β–²  β”œ chunks/main-app-b5b471d2d5990a4e.js    211 B
2023-04-25T14:39:02.992589Z β–²  β”” chunks/webpack-d865449db0df187c.js     1.68 kB
2023-04-25T14:39:02.992802Z β–²  
2023-04-25T14:39:02.993022Z β–²  Route (pages)                              Size     First Load JS
2023-04-25T14:39:02.993175Z β–²  ─ β—‹ /404                                   178 B          87.1 kB
2023-04-25T14:39:02.993294Z β–²  + First Load JS shared by all              86.9 kB
2023-04-25T14:39:02.993404Z β–²  β”œ chunks/main-b5f0d8ee971aa431.js        85.1 kB
2023-04-25T14:39:02.993509Z β–²  β”œ chunks/pages/_app-c544d6df833bfd4a.js  192 B
2023-04-25T14:39:02.993614Z β–²  β”” chunks/webpack-d865449db0df187c.js     1.68 kB
2023-04-25T14:39:02.993731Z β–²  ℇ  (Streaming)  server-side renders with streaming (uses React 18 SSR streaming or Server Components)
2023-04-25T14:39:02.993859Z β–²  β—‹  (Static)     automatically rendered as static HTML (uses no initial props)
2023-04-25T14:39:07.316935Z β–²  Traced Next.js server files in: 3.594s
2023-04-25T14:39:09.199025Z β–²  Created all serverless functions in: 1.882s
2023-04-25T14:39:09.387271Z β–²  Collected static files (public/, static/, .next/static): 6.95ms
2023-04-25T14:39:10.340255Z β–²  Build Completed in .vercel/output [38s]
2023-04-25T14:39:10.45724Z  ⚑️ Completed `npx vercel build`.
2023-04-25T14:39:11.472529Z ⚑️ Generated '.vercel/output/static/_worker.js'.
2023-04-25T14:39:11.512154Z Finished
2023-04-25T14:39:11.512752Z Found _worker.js in output directory. Uploading.
2023-04-25T14:39:12.360143Z ✘ [ERROR] 1 error(s) and 0 warning(s) when compiling Worker.
2023-04-25T14:39:12.36093Z  
2023-04-25T14:39:12.361545Z 
2023-04-25T14:39:12.364346Z 
2023-04-25T14:39:12.366095Z ✘ [ERROR] Could not resolve "node:buffer"
2023-04-25T14:39:12.366852Z 
2023-04-25T14:39:12.367031Z     _worker.js:2:2565:
2023-04-25T14:39:12.367168Z       2 β”‚ ...ar mr={};import*as e2 from"node:buffer";var cc=Te(()=>{B();H();g...
2023-04-25T14:39:12.36732Z          β•΅                              ~~~~~~~~~~~~~
2023-04-25T14:39:12.367927Z 
2023-04-25T14:39:12.36813Z    The package "node:buffer" wasn't found on the file system but is built into node.
2023-04-25T14:39:12.368307Z   Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.
2023-04-25T14:39:12.368426Z 
2023-04-25T14:39:12.368639Z 
2023-04-25T14:39:12.368795Z ✘ [ERROR] Build failed with 1 error:
2023-04-25T14:39:12.368911Z 
2023-04-25T14:39:12.369028Z   _worker.js:2:2565: ERROR: Could not resolve "node:buffer"
2023-04-25T14:39:12.369149Z 
2023-04-25T14:39:12.369261Z 
2023-04-25T14:39:12.380787Z Failed building Pages Functions.
2023-04-25T14:39:13.294938Z Failed: an internal error occurred
Screenshot 2023-04-25 at 7 51 14 AM
dario-piotrowicz commented 1 year ago

Thank you again @dario-piotrowicz for your prompt fix! @hjaber my pleasure, it was my dumb mistake that cause the issue in the first place after all πŸ˜…

dario-piotrowicz commented 1 year ago

@hjaber dang, I'm sorry things are still not working for you, what you're seeing is quite weird!

from your logs (import*as e2 fromοΏ½[32m"node:buffer") I can see that the import is correctly converted now, so that's good at least

Regarding the issue you're facing I think it is an issue with wrangler/pages in general and that it needs to be fixed upstream, but I'll investigate (so I reopened the issue)

You definitely should not need a wrangler.toml file for your Pages application but you could try to add one with the nodejs_compat flag just to see if that for some reason fixes the bug temporarily until we fix the upstream issue (just trying that can't hurt πŸ™‚)

dario-piotrowicz commented 1 year ago

PS: I just tried adding a wrangler.toml file doesn't seem to help in any way, the message might be just misleading

hjaber commented 1 year ago

PS: I just tried adding a wrangler.toml file doesn't seem to help in any way, the message might be just misleading

I had the same result. As far as I can tell, I'm using default settings and it is more or less a fresh install so nothing stands out as strange to me. I appreciate your help in triaging the issue and updating it

dario-piotrowicz commented 1 year ago

@hjaber sorry it's taking a while, we're working on it πŸ˜“

Not ideal but until my PR gets merged you can just use the prerelease (https://github.com/cloudflare/workers-sdk/pull/3133#issuecomment-1533440046) to manually deploy your app (making sure that you've got nodejs_compat set in the dashboard)

atali commented 1 year ago

Hello @dario-piotrowicz , do you have an ETA when your fix will be merged into production ?

dario-piotrowicz commented 1 year ago

@atali my fix already got merged so if you use wrangler 2.20.0 you should be able to deploy with that πŸ™‚ (granted that you've added the nodejs_compat flag for your project)

If you want to use the Pages github integration that needs also the Pages build image update to be released, I think it should happen tomorrow, I'll let know you when it is πŸ™‚

atali commented 1 year ago

I was talking to deploy from the Clouflare Pages website. Thank you so much @dario-piotrowicz

dario-piotrowicz commented 1 year ago

@atali ah ok :slightly_smiling_face:, I just checked and tested it out and the CI build image has been updated and the automatic deployment works fine now :slightly_smiling_face::+1:

atali commented 1 year ago

It works ! you rocks :-)

hjaber commented 1 year ago

@atali ah ok :slightly_smiling_face:, I just checked and tested it out and the CI build image has been updated and the automatic deployment works fine now :slightly_smiling_face::+1:

Thank you for following through on this issue!

dario-piotrowicz commented 1 year ago

@hjaber no problem :slightly_smiling_face:

(PS: please try not to use the latest beta release as we're currently having some issues there :sweat: https://github.com/cloudflare/next-on-pages/issues/54#issuecomment-1543888672, 0.10.1 should work as expected :slightly_smiling_face:)

mountainash commented 1 year ago

I can confirm that I'm able to run locally, and on CF Pages with the follow deps versions:

    "@cloudflare/next-on-pages": "0.10.1",
    "next": "13.4.2",
    "vercel": "29.3.0",
    "wrangler": "2.20.0"

I know this issue is titled NextJS v13.3.1, but it was created when that was the latest, now NextJS is up to v13.4.2

g-wozniak commented 8 months ago

Sadly the bug still persists for @sveltejs/adapter-cloudflare :( Glad. you managed to solve it for Next.