After successfully building my Next.js app on AWS CodeBuild for several years, I decided to update it from version 13 to version 14. However, following this update, my builds started failing with an error message:
unhandledRejection ReferenceError: Headers is not defined
at Object.<anonymous> (/codebuild/output/src3629032198/src/node_modules/next/dist/server/web/spec-extension/adapters/headers.js:32:30)
at Module._compile (node:internal/modules/cjs/loader:1198:14)
...
It seems like the error lies within the Next.js codebase itself and not in my own code which has no reference to "Headers". Has anyone experienced a similar issue and can offer some suggestions for a solution?
Potentially helpful information:
My NPM build script in package.json is:
"build": "NEXT_PUBLIC_VERSION=$npm_package_version NEXT_PUBLIC_DATE=$(date +%s) next build"
My buildspec.yml file contains:
version: 0.2
phases:
pre_build:
commands:
- npm install
build:
commands:
- npm run build
artifacts:
files:
- '**/*'
base-directory: build
And my next.config.js includes:
const isLocal = process.env.NODE_ENV === 'development';
const nextConfig = {
trailingSlash: true,
output: 'export',
distDir: 'build',
...
};
module.exports = nextConfig;
Note:
In Next.js 13, my build script included "&& next export -o build" at the end but after upgrading to version 14, instructions suggested moving this to next.config.js:
output: 'export',
distDir: 'build',