Build nextjs14 - nodejs20 fails

the build on netlify fails, locally it works.
Could the problem be that I use node v 20.9.0 locally and netlify environment supports only node 18?
When is support for node 20 available or do I have to downgrade nodejs?

8:13:13 AM: Netlify Build
8:13:13 AM: ────────────────────────────────────────────────────────────────
8:13:13 AM: ​
8:13:13 AM: ❯ Version
8:13:13 AM: @netlify/build 29.24.3
8:13:13 AM: ​
8:13:13 AM: ❯ Flags
8:13:13 AM: baseRelDir: true
8:13:13 AM: buildId: 6541fa5cb4a8d20008081649
8:13:13 AM: deployId: 6541fa5cb4a8d2000808164b
8:13:13 AM: ​
8:13:13 AM: ❯ Current directory
8:13:13 AM: /opt/build/repo
8:13:13 AM: ​
8:13:13 AM: ❯ Config file
8:13:13 AM: /opt/build/repo/netlify.toml
8:13:13 AM: ​
8:13:13 AM: ❯ Context
8:13:13 AM: deploy-preview
8:13:13 AM: ​
8:13:13 AM: ❯ Using Next.js Runtime - v4.41.1
8:13:15 AM: ​
8:13:15 AM: @netlify/plugin-nextjs (onPreBuild event)
8:13:15 AM: ────────────────────────────────────────────────────────────────
8:13:15 AM: ​
8:13:15 AM: No Next.js cache to restore.
8:13:15 AM: Netlify configuration property “build.environment.NEXT_PRIVATE_TARGET” value changed.
8:13:15 AM: ​
8:13:15 AM: (@netlify/plugin-nextjs onPreBuild completed in 61ms)
8:13:15 AM: ​
8:13:15 AM: build.command from netlify.toml
8:13:15 AM: ────────────────────────────────────────────────────────────────
8:13:15 AM: ​
8:13:15 AM: $ npm run prod
8:13:15 AM: > info@1.0.0 prod
8:13:15 AM: > npm run build
8:13:15 AM: > info@1.0.0 build
8:13:15 AM: > next build
8:13:15 AM: unhandledRejection ReferenceError: Headers is not defined
8:13:15 AM: at /opt/build/repo/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:8846
8:13:15 AM: at /opt/build/repo/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:41308
8:13:15 AM: at Object. (/opt/build/repo/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:41329)
8:13:15 AM: at Module._compile (node:internal/modules/cjs/loader:1198:14)
8:13:15 AM: at Object.Module._extensions…js (node:internal/modules/cjs/loader:1252:10)
8:13:15 AM: at Module.load (node:internal/modules/cjs/loader:1076:32)
8:13:15 AM: at Function.Module._load (node:internal/modules/cjs/loader:911:12)
8:13:15 AM: at Module.require (node:internal/modules/cjs/loader:1100:19)
8:13:15 AM: at Module.mod.require (/opt/build/repo/node_modules/next/dist/server/require-hook.js:64:28)
8:13:15 AM: at require (node:internal/modules/cjs/helpers:119:18)
8:13:15 AM: ​
8:13:15 AM: “build.command” failed
8:13:15 AM: ────────────────────────────────────────────────────────────────
8:13:15 AM: ​
8:13:15 AM: Error message
8:13:15 AM: Command failed with exit code 1: npm run prod (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
8:13:15 AM: ​
8:13:15 AM: Error location
8:13:15 AM: In build.command from netlify.toml:
8:13:15 AM: npm run prod
8:13:15 AM: ​
8:13:15 AM: Resolved config
8:13:15 AM: build:
8:13:15 AM: command: npm run prod
8:13:15 AM: commandOrigin: config
8:13:15 AM: environment:
8:13:15 AM: - REVIEW_ID
8:13:15 AM: - NEXT_PRIVATE_TARGET
8:13:15 AM: publish: /opt/build/repo/.next
8:13:15 AM: publishOrigin: ui
8:13:15 AM: functionsDirectory: /opt/build/repo/lambda
8:13:15 AM: plugins:
8:13:15 AM: - inputs: {}
8:13:15 AM: origin: ui
8:13:15 AM: package: “@netlify/plugin-nextjs”
8:13:16 AM: Failed during stage “building site”: Build script returned non-zero exit code: 2
8:13:16 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
8:13:16 AM: Failing build: Failed to build site
8:13:16 AM: Finished processing build request in 45.882s

We support Node 20, though I’d recommend using Node 18 as most other tools would still be supporting Node 18 and not 20. In any case, the issue in that part, nothing to do with Node 18 or 20. You’re using a variable that you’ve not defined. It’s a code issue, not a Netlify issue.

I’m getting the same error after upgrading to latest Next & Nextra. Afaik this is not a user error. Or at least nothing changed regarding headers @hrishikesh

see the open source PR Add katex support by nikgraf · Pull Request #1 · serenity-kit/documentation · GitHub

I also tried without enabling latex and just the dependency updates and in addition cleaned the cache. Same story. Could it have to do with @netlify/plugin-nextjs - npm?

The code is failing in next build. It has nothing to do with the plugin. If your code doesn’t have Headers, it’s either coming from Next.js or one of your dependencies. Based on the stacktrace, it appears to be related to Next.js itself.

This likely doesn’t have anything to do with Netlify.

Fixed it for my case by upgrade to Node 18 and then also tried 20 which worked and now sticked to. The builds that failed used Now using node v16.20.2 (npm v8.19.4).

1 Like