Nextjs Api: TypeError: Cannot read properties of undefined (reading 'headers')

My website: https://main--weedle.netlify.app/

I deployed a nextjs app that contains api’s and also uses environment variables. I have added the environment variables to their section in the netlify console and the app also deploys without any issues. But I keep getting this error when i try to call my single api.

The app works perfectly locally but when running it in dev and using netlify cli, but it does not work once its deployed to netlify.

The full api route is https://main–weedle.netlify.app/api/pre-launch/register

Hey there, @WeedleApp :wave:

Thanks so much for reaching out. Can you share your repository and also your full deploy log so that we can look into this further? This will help us have more context when debugging.

Thanks!

Hi @hillary,

Apologies this took a bit of time to respond. Here is the url to the commit that has this issue

https://github.com/weedle-app/weedle-landing-page/commit/ba1183566a5cc1aaadc9369a9295e5d5eabca468

Since then as a temporary fix, i have had to downgrade the nextjs version which seems to solve the problem, but I have lost hot reload and a bunch of functionality. Note as well that this works well if deployed to vercel.

Here is the deploy log.

12:17:10 AM: Build ready to start
12:17:31 AM: build-image version: 0e2f4c52031ab562db66aec633308326e3b108d0 (focal)
12:17:31 AM: build-image tag: focal
12:17:31 AM: buildbot version: 584b2b7b74f1e479e0403f1e60769a2080b692a6
12:17:31 AM: Fetching cached dependencies
12:17:31 AM: Starting to download cache of 513.0MB
12:17:35 AM: Finished downloading cache in 3.917605822s
12:17:35 AM: Starting to extract cache
12:17:40 AM: Finished extracting cache in 4.626091766s
12:17:40 AM: Finished fetching cache in 8.657000778s
12:17:40 AM: Starting to prepare the repo for build
12:17:40 AM: Preparing Git Reference refs/heads/main
12:17:41 AM: Parsing package.json dependencies
12:17:42 AM: Starting build script
12:17:42 AM: Installing dependencies
12:17:42 AM: Python version set to 2.7
12:17:42 AM: Started restoring cached node version
12:17:44 AM: Finished restoring cached node version
12:17:44 AM: v16.16.0 is already installed.
12:17:44 AM: Now using node v16.16.0 (npm v8.11.0)
12:17:44 AM: Started restoring cached build plugins
12:17:44 AM: Finished restoring cached build plugins
12:17:45 AM: Attempting ruby version 2.7.2, read from environment
12:17:46 AM: Using ruby version 2.7.2
12:17:46 AM: Using PHP version 8.0
12:17:46 AM: Started restoring cached yarn cache
12:17:48 AM: Finished restoring cached yarn cache
12:17:49 AM: No yarn workspaces detected
12:17:49 AM: Started restoring cached node modules
12:17:49 AM: Finished restoring cached node modules
12:17:49 AM: Installing NPM modules using Yarn version 1.22.10
12:17:49 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
12:17:49 AM: npm WARN config location in the cache, and they are managed by
12:17:49 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
12:17:50 AM: yarn install v1.22.10
12:17:50 AM: [1/4] Resolving packages...
12:17:50 AM: success Already up-to-date.
12:17:50 AM: Done in 0.35s.
12:17:50 AM: NPM modules installed using Yarn
12:17:50 AM: Started restoring cached go cache
12:17:50 AM: Finished restoring cached go cache
12:17:50 AM: Installing Go version 1.16.5 (requested 1.16.5)
12:17:56 AM: unset GOOS;
12:17:56 AM: unset GOARCH;
12:17:56 AM: export GOROOT='/opt/buildhome/.gimme/versions/go1.16.5.linux.amd64';
12:17:56 AM: export PATH="/opt/buildhome/.gimme/versions/go1.16.5.linux.amd64/bin:${PATH}";
12:17:56 AM: go version >&2;
12:17:56 AM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.16.5.linux.amd64.env"
12:17:56 AM: go version go1.16.5 linux/amd64
12:17:56 AM: Installing missing commands
12:17:56 AM: Verify run directory
12:17:57 AM: ​
12:17:57 AM: ────────────────────────────────────────────────────────────────
12:17:57 AM:   Netlify Build                                                 
12:17:57 AM: ────────────────────────────────────────────────────────────────
12:17:57 AM: ​
12:17:57 AM: ❯ Version
12:17:57 AM:   @netlify/build 27.5.0
12:17:57 AM: ​
12:17:57 AM: ❯ Flags
12:17:57 AM:   baseRelDir: true
12:17:57 AM:   buildId: 62df86e609bff20008f437a8
12:17:57 AM:   deployId: 62df86e609bff20008f437aa
12:17:58 AM: ​
12:17:58 AM: ❯ Current directory
12:17:58 AM:   /opt/build/repo
12:17:58 AM: ​
12:17:58 AM: ❯ Config file
12:17:58 AM:   No config file was defined: using default values.
12:17:58 AM: ​
12:17:58 AM: ❯ Context
12:17:58 AM:   production
12:17:58 AM: ​
12:17:58 AM: ❯ Loading plugins
12:17:58 AM:    - @netlify/plugin-nextjs@3.9.2 from Netlify app
12:17:58 AM: ​
12:17:58 AM: ❯ Outdated plugins
12:17:58 AM:    - @netlify/plugin-nextjs@3.9.2: latest version is 4.12.2
12:17:58 AM:      Migration guide: https://ntl.fyi/next-plugin-migration
12:17:58 AM:      To upgrade this plugin, please uninstall and re-install it from the Netlify plugins directory (https://app.netlify.com/plugins)
12:17:59 AM: ​
12:17:59 AM: ────────────────────────────────────────────────────────────────
12:17:59 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
12:17:59 AM: ────────────────────────────────────────────────────────────────
12:17:59 AM: ​
12:17:59 AM: Using Next.js 12.1.5
12:17:59 AM: Warning: support for Next.js >=11.1.0 is experimental
12:17:59 AM: warn  - The `target` config is deprecated and will be removed in a future version.
12:17:59 AM: See more info here https://nextjs.org/docs/messages/deprecated-target-config
12:18:00 AM: Next.js cache restored.
12:18:00 AM: ​
12:18:00 AM: (@netlify/plugin-nextjs onPreBuild completed in 378ms)
12:18:00 AM: ​
12:18:00 AM: ────────────────────────────────────────────────────────────────
12:18:00 AM:   2. Build command from Netlify app                             
12:18:00 AM: ────────────────────────────────────────────────────────────────
12:18:00 AM: ​
12:18:00 AM: $ yarn build
12:18:00 AM: yarn run v1.22.10
12:18:00 AM: $ next build
12:18:01 AM: warn  - The `target` config is deprecated and will be removed in a future version.
12:18:01 AM: See more info here https://nextjs.org/docs/messages/deprecated-target-config
12:18:01 AM: info  - Checking validity of types...
12:18:07 AM: info  - Creating an optimized production build...
12:18:08 AM: warn - As of Tailwind CSS v2.2, `lightBlue` has been renamed to `sky`.
12:18:08 AM: warn - Update your configuration file to silence this warning.
12:18:08 AM: warn - As of Tailwind CSS v3.0, `warmGray` has been renamed to `stone`.
12:18:08 AM: warn - Update your configuration file to silence this warning.
12:18:08 AM: warn - As of Tailwind CSS v3.0, `trueGray` has been renamed to `neutral`.
12:18:08 AM: warn - Update your configuration file to silence this warning.
12:18:08 AM: warn - As of Tailwind CSS v3.0, `coolGray` has been renamed to `gray`.
12:18:08 AM: warn - Update your configuration file to silence this warning.
12:18:08 AM: warn - As of Tailwind CSS v3.0, `blueGray` has been renamed to `slate`.
12:18:08 AM: warn - Update your configuration file to silence this warning.
12:18:12 AM: info  - Compiled successfully
12:18:12 AM: info  - Collecting page data...
12:18:13 AM: info  - Generating static pages (0/8)
12:18:13 AM: info  - Generating static pages (2/8)
12:18:13 AM: info  - Generating static pages (4/8)
12:18:13 AM: info  - Generating static pages (6/8)
12:18:13 AM: info  - Generating static pages (8/8)
12:18:14 AM: info  - Finalizing page optimization...
12:18:14 AM: Page                                       Size     First Load JS
12:18:14 AM: ┌ ○ /                                      16.6 kB         112 kB
12:18:14 AM: ├   └ css/b0dcb53e35005dd5.css             1.36 kB
12:18:14 AM: ├   /_app                                  0 B            74.7 kB
12:18:14 AM: ├ ○ /404                                   192 B          74.9 kB
12:18:14 AM: ├ λ /api/pre-launch/register               0 B            74.7 kB
12:18:14 AM: ├ ○ /Landing/CallToAction                  1.71 kB        96.7 kB
12:18:14 AM: ├ ○ /Landing/Explainer                     1.67 kB        76.4 kB
12:18:14 AM: ├   └ css/e666a1e623ca3c99.css             348 B
12:18:14 AM: ├ ○ /Landing/Explainer/ExplainerBox        652 B          75.3 kB
12:18:14 AM: ├ ○ /Landing/Features                      2.6 kB         82.6 kB
12:18:14 AM: └ ○ /Landing/Features/FeatureItem          820 B          80.9 kB
12:18:14 AM: + First Load JS shared by all              74.7 kB
12:18:14 AM:   ├ chunks/framework-a87821de553db91d.js   45 kB
12:18:14 AM:   ├ chunks/main-f4ae3437c92c1efc.js        28.3 kB
12:18:14 AM:   ├ chunks/pages/_app-0e377e13956df014.js  504 B
12:18:14 AM:   ├ chunks/webpack-5752944655d749a0.js     840 B
12:18:14 AM:   └ css/432876239901b612.css               3.41 kB
12:18:14 AM: λ  (Lambda)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
12:18:14 AM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
12:18:14 AM: Done in 13.81s.
12:18:14 AM: ​
12:18:14 AM: (build.command completed in 14s)
12:18:14 AM: ​
12:18:14 AM: ────────────────────────────────────────────────────────────────
12:18:14 AM:   3. @netlify/plugin-nextjs (onBuild event)                     
12:18:14 AM: ────────────────────────────────────────────────────────────────
12:18:14 AM: ​
12:18:14 AM: Detected Next.js site. Copying files...
12:18:14 AM: ** Running Next on Netlify package **
12:18:14 AM: 🚀 Next on Netlify 🚀
12:18:14 AM: 🌍️ Copying public folder to /opt/build/repo/build
12:18:14 AM: 💼 Copying static NextJS assets to /opt/build/repo/build
12:18:14 AM: 💫 Setting up API endpoints as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:18:14 AM: 💫 Setting up pages with getInitialProps as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:18:14 AM: 💫 Setting up pages with getServerSideProps as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:18:14 AM: 🔥 Copying pre-rendered pages with getStaticProps and JSON data to /opt/build/repo/build
12:18:14 AM: 💫 Setting up pages with getStaticProps and fallback: true as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:18:14 AM: 💫 Setting up pages with getStaticProps and revalidation interval as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:18:14 AM: 🔥 Copying pre-rendered pages without props to /opt/build/repo/build
12:18:14 AM: Building 8 pages
12:18:14 AM: 🔀 Setting up redirects
12:18:14 AM: 🔀 Setting up headers
12:18:14 AM: ✅ Success! All done!
12:18:14 AM: ​
12:18:14 AM: (@netlify/plugin-nextjs onBuild completed in 103ms)
12:18:14 AM: ​
12:18:14 AM: ────────────────────────────────────────────────────────────────
12:18:14 AM:   4. Functions bundling                                         
12:18:14 AM: ────────────────────────────────────────────────────────────────
12:18:14 AM: ​
12:18:14 AM: Packaging Functions from .netlify/functions-internal directory:
12:18:14 AM:  - next_api_prelaunch_register/next_api_prelaunch_register.js
12:18:14 AM:  - next_image/next_image.js
12:18:14 AM: ​
12:18:16 AM: ​
12:18:16 AM: (Functions bundling completed in 1.8s)
12:18:16 AM: ​
12:18:16 AM: ────────────────────────────────────────────────────────────────
12:18:16 AM:   5. @netlify/plugin-nextjs (onPostBuild event)                 
12:18:16 AM: ────────────────────────────────────────────────────────────────
12:18:16 AM: ​
12:18:16 AM: Next.js cache saved.
12:18:16 AM: 
12:18:16 AM: For faster deploy times, build IDs should be set to a static value.
12:18:16 AM: To do this, set generateBuildId: () => 'build' in your next.config.js
12:18:16 AM: ​
12:18:16 AM: (@netlify/plugin-nextjs onPostBuild completed in 282ms)
12:18:16 AM: ​
12:18:16 AM: ────────────────────────────────────────────────────────────────
12:18:16 AM:   6. Deploy site                                                
12:18:16 AM: ────────────────────────────────────────────────────────────────
12:18:16 AM: ​
12:18:16 AM: Starting to deploy site from 'build'
12:18:16 AM: Creating deploy tree 
12:18:16 AM: Creating deploy upload records
12:18:16 AM: 7 new files to upload
12:18:16 AM: 1 new functions to upload
12:18:22 AM: Site deploy was successfully initiated
12:18:22 AM: ​
12:18:22 AM: (Deploy site completed in 6.5s)
12:18:22 AM: ​
12:18:22 AM: ────────────────────────────────────────────────────────────────
12:18:22 AM:   Netlify Build Complete                                        
12:18:22 AM: ────────────────────────────────────────────────────────────────
12:18:22 AM: ​
12:18:22 AM: (Netlify Build completed in 24.9s)
12:18:23 AM: Starting post processing
12:18:23 AM: Caching artifacts
12:18:23 AM: Started saving node modules
12:18:23 AM: Finished saving node modules
12:18:23 AM: Started saving build plugins
12:18:23 AM: Finished saving build plugins
12:18:23 AM: Started saving yarn cache
12:18:23 AM: Post processing - HTML
12:18:24 AM: Post processing - header rules
12:18:24 AM: Post processing - redirect rules
12:18:24 AM: Post processing done
12:18:25 AM: Finished saving yarn cache
12:18:25 AM: Started saving pip cache
12:18:25 AM: Finished saving pip cache
12:18:25 AM: Started saving emacs cask dependencies
12:18:25 AM: Finished saving emacs cask dependencies
12:18:25 AM: Started saving maven dependencies
12:18:25 AM: Finished saving maven dependencies
12:18:25 AM: Started saving boot dependencies
12:18:25 AM: Finished saving boot dependencies
12:18:25 AM: Started saving rust rustup cache
12:18:25 AM: Finished saving rust rustup cache
12:18:25 AM: Started saving go dependencies
12:18:25 AM: Finished saving go dependencies
12:18:25 AM: Build script success
12:18:27 AM: Site is live ✨
12:18:32 AM: Uploading Cache of size 513.4MB
12:18:38 AM: Finished processing build request in 1m6.916073937s

Mind upgrading to latest version of the plugin to try? The link to migration guide is right there in the build log.

1 Like

Thank you @hrishikesh that solved it.