Build failure with NextJS and API data

Hey all!
My domain is: superlative-arithmetic-f35229

I have been pushing code from my GitHub repo successfully for a couple months, but recently I wanted to make a new repo and Netlify site to show a client. Now I’m running into build issues on the new site.

My project is using NextJS and static page generation, using the Notion API as a CMS. As I said previously, I had a page running successfully before but since switching to a new repo and site, the problems have started.

There is an error message about the API token from Notion, but everything runs fine both locally and on Postman with no API key issues.

Any help at all would be fantastic as I feel I’ve tried so many solutions that haven’t worked.

1:07:43 PM: build-image version: d55944c047ef0aee2ac01971edde72cb2a50acfe (focal)
1:07:43 PM: build-image tag: v4.16.1
1:07:43 PM: buildbot version: 33f88475bcf3426608b49b7681d295235efb41f1
1:07:44 PM: Fetching cached dependencies
1:07:44 PM: Failed to fetch cache, continuing with build
1:07:44 PM: Starting to prepare the repo for build
1:07:44 PM: No cached dependencies found. Cloning fresh repo
1:07:44 PM: git clone --filter=blob:none https://github.com/ramclean/megazerosite
1:07:45 PM: Preparing Git Reference refs/heads/main
1:07:45 PM: Parsing package.json dependencies
1:07:46 PM: Starting build script
1:07:46 PM: Installing dependencies
1:07:46 PM: Python version set to 2.7
1:07:47 PM: v16.19.0 is already installed.
1:07:47 PM: Now using node v16.19.0 (npm v8.19.3)
1:07:47 PM: Enabling node corepack
1:07:47 PM: Started restoring cached build plugins
1:07:47 PM: Finished restoring cached build plugins
1:07:47 PM: Attempting ruby version 2.7.2, read from environment
1:07:48 PM: Using ruby version 2.7.2
1:07:48 PM: Using PHP version 8.0
1:07:48 PM: No npm workspaces detected
1:07:48 PM: Started restoring cached node modules
1:07:48 PM: Finished restoring cached node modules
1:07:48 PM: Installing NPM modules using NPM version 8.19.3
1:07:52 PM: added 59 packages, and audited 60 packages in 3s
1:07:52 PM: 4 packages are looking for funding
1:07:52 PM:   run `npm fund` for details
1:07:52 PM: found 0 vulnerabilities
1:07:52 PM: NPM modules installed
1:07:52 PM: Creating package sha
1:07:52 PM: Started restoring cached go cache
1:07:52 PM: Finished restoring cached go cache
1:07:52 PM: go version go1.19.4 linux/amd64
1:07:52 PM: Detected 1 framework(s)
1:07:52 PM: "next" at version "13.0.4"
1:07:52 PM: Installing missing commands
1:07:52 PM: Verify run directory
1:07:52 PM: Section completed: initializing
1:07:54 PM: ​
1:07:54 PM:   Netlify Build                                                 
1:07:54 PM: ────────────────────────────────────────────────────────────────
1:07:54 PM: ​
1:07:54 PM: ❯ Version
1:07:54 PM:   @netlify/build 29.2.1
1:07:54 PM: ​
1:07:54 PM: ❯ Flags
1:07:54 PM:   baseRelDir: true
1:07:54 PM:   buildId: 63a44878445fd80008c92f86
1:07:54 PM:   deployId: 63a44878445fd80008c92f88
1:07:54 PM: ​
1:07:54 PM: ❯ Current directory
1:07:54 PM:   /opt/build/repo
1:07:54 PM: ​
1:07:54 PM: ❯ Config file
1:07:54 PM:   No config file was defined: using default values.
1:07:54 PM: ​
1:07:54 PM: ❯ Context
1:07:54 PM:   production
1:07:54 PM: ​
1:07:54 PM: ❯ Installing plugins
1:07:54 PM:    - @netlify/plugin-nextjs@4.29.4
1:08:00 PM: ​
1:08:00 PM: ❯ Using Next.js Runtime - v4.29.4
1:08:02 PM: ​
1:08:02 PM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
1:08:02 PM: ────────────────────────────────────────────────────────────────
1:08:02 PM: ​
1:08:02 PM: No Next.js cache to restore.
1:08:02 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
1:08:02 PM: ​
1:08:02 PM: (@netlify/plugin-nextjs onPreBuild completed in 14ms)
1:08:02 PM: ​
1:08:02 PM:   2. Build command from Netlify app                             
1:08:02 PM: ────────────────────────────────────────────────────────────────
1:08:02 PM: ​
1:08:02 PM: $ npm run build
1:08:02 PM: > megazeronext@0.1.0 build
1:08:02 PM: > next build
1:08:02 PM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
1:08:02 PM: info  - Linting and checking validity of types...
1:08:03 PM: info  - Creating an optimized production build...
1:08:07 PM: info  - Compiled successfully
1:08:07 PM: info  - Collecting page data...
1:08:11 PM: info  - Generating static pages (0/10)
1:08:12 PM: info  - Generating static pages (2/10)
1:08:12 PM: info  - Generating static pages (4/10)
1:08:12 PM: @notionhq/client warn: request fail { code: 'unauthorized', message: 'API token is invalid.' }
1:08:12 PM: Error occurred prerendering page "/Videos". Read more: https://nextjs.org/docs/messages/prerender-error
1:08:12 PM: APIResponseError: API token is invalid.
1:08:12 PM:     at buildRequestError (/opt/build/repo/node_modules/@notionhq/client/build/src/errors.js:162:16)
1:08:12 PM:     at Client.request (/opt/build/repo/node_modules/@notionhq/client/build/src/Client.js:342:54)
1:08:12 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
1:08:12 PM:     at async getStaticProps (/opt/build/repo/.next/server/chunks/7.js:55:20)
1:08:12 PM:     at async renderToHTML (/opt/build/repo/node_modules/next/dist/server/render.js:384:20)
1:08:12 PM:     at async /opt/build/repo/node_modules/next/dist/export/worker.js:248:32
1:08:12 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:12 PM: @notionhq/client warn: request fail { code: 'unauthorized', message: 'API token is invalid.' }
1:08:12 PM: Error occurred prerendering page "/Releases". Read more: https://nextjs.org/docs/messages/prerender-error
1:08:12 PM: APIResponseError: API token is invalid.
1:08:12 PM:     at buildRequestError (/opt/build/repo/node_modules/@notionhq/client/build/src/errors.js:162:16)
1:08:12 PM:     at Client.request (/opt/build/repo/node_modules/@notionhq/client/build/src/Client.js:342:54)
1:08:12 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
1:08:12 PM:     at async getStaticProps (/opt/build/repo/.next/server/chunks/564.js:53:22)
1:08:12 PM:     at async renderToHTML (/opt/build/repo/node_modules/next/dist/server/render.js:384:20)
1:08:12 PM:     at async /opt/build/repo/node_modules/next/dist/export/worker.js:248:32
1:08:12 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:12 PM: info  - Generating static pages (7/10)
1:08:12 PM: @notionhq/client warn: request fail { code: 'unauthorized', message: 'API token is invalid.' }
1:08:12 PM: Error occurred prerendering page "/Shows". Read more: https://nextjs.org/docs/messages/prerender-error
1:08:12 PM: APIResponseError: API token is invalid.
1:08:12 PM:     at buildRequestError (/opt/build/repo/node_modules/@notionhq/client/build/src/errors.js:162:16)
1:08:12 PM:     at Client.request (/opt/build/repo/node_modules/@notionhq/client/build/src/Client.js:342:54)
1:08:12 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
1:08:12 PM:     at async getStaticProps (/opt/build/repo/.next/server/chunks/652.js:131:19)
1:08:12 PM:     at async renderToHTML (/opt/build/repo/node_modules/next/dist/server/render.js:384:20)
1:08:12 PM:     at async /opt/build/repo/node_modules/next/dist/export/worker.js:248:32
1:08:12 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:12 PM: @notionhq/client warn: request fail { code: 'unauthorized', message: 'API token is invalid.' }
1:08:12 PM: Error occurred prerendering page "/Bio". Read more: https://nextjs.org/docs/messages/prerender-error
1:08:12 PM: APIResponseError: API token is invalid.
1:08:12 PM:     at buildRequestError (/opt/build/repo/node_modules/@notionhq/client/build/src/errors.js:162:16)
1:08:12 PM:     at Client.request (/opt/build/repo/node_modules/@notionhq/client/build/src/Client.js:342:54)
1:08:12 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
1:08:12 PM:     at async getStaticProps (/opt/build/repo/.next/server/pages/Bio.js:46:17)
1:08:12 PM:     at async renderToHTML (/opt/build/repo/node_modules/next/dist/server/render.js:384:20)
1:08:12 PM:     at async /opt/build/repo/node_modules/next/dist/export/worker.js:248:32
1:08:12 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:13 PM: @notionhq/client warn: request fail { code: 'unauthorized', message: 'API token is invalid.' }
1:08:13 PM: Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
1:08:13 PM: APIResponseError: API token is invalid.
1:08:13 PM:     at buildRequestError (/opt/build/repo/node_modules/@notionhq/client/build/src/errors.js:162:16)
1:08:13 PM:     at Client.request (/opt/build/repo/node_modules/@notionhq/client/build/src/Client.js:342:54)
1:08:13 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
1:08:13 PM:     at async getStaticProps (/opt/build/repo/.next/server/pages/index.js:124:19)
1:08:13 PM:     at async renderToHTML (/opt/build/repo/node_modules/next/dist/server/render.js:384:20)
1:08:13 PM:     at async /opt/build/repo/node_modules/next/dist/export/worker.js:248:32
1:08:13 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:13 PM: info  - Generating static pages (10/10)
1:08:13 PM: > Build error occurred
1:08:13 PM: Error: Export encountered errors on following paths:
1:08:13 PM: 	/
1:08:13 PM: 	/Bio
1:08:13 PM: 	/Releases
1:08:13 PM: 	/Shows
1:08:13 PM: 	/Videos
1:08:13 PM:     at /opt/build/repo/node_modules/next/dist/export/index.js:408:19
1:08:13 PM:     at runMicrotasks (<anonymous>)
1:08:14 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
1:08:13 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
1:08:13 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:13 PM:     at async /opt/build/repo/node_modules/next/dist/build/index.js:1331:21
1:08:13 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:13 PM:     at async /opt/build/repo/node_modules/next/dist/build/index.js:1191:17
1:08:13 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20)
1:08:13 PM:     at async Object.build [as default] (/opt/build/repo/node_modules/next/dist/build/index.js:65:29)
1:08:13 PM: ​
1:08:13 PM:   "build.command" failed                                        
1:08:13 PM: ────────────────────────────────────────────────────────────────
1:08:13 PM: ​
1:08:13 PM:   Error message
1:08:13 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
1:08:13 PM: ​
1:08:13 PM:   Error location
1:08:13 PM:   In Build command from Netlify app:
1:08:13 PM:   npm run build
1:08:13 PM: ​
1:08:13 PM:   Resolved config
1:08:13 PM:   build:
1:08:13 PM:     command: npm run build
1:08:13 PM:     commandOrigin: ui
1:08:13 PM:     environment:
1:08:13 PM:       - NEXT_PRIVATE_TARGET
1:08:13 PM:     publish: /opt/build/repo/.next
1:08:13 PM:     publishOrigin: ui
1:08:13 PM:   plugins:
1:08:13 PM:     - inputs: {}
1:08:13 PM:       origin: ui
1:08:13 PM:       package: '@netlify/plugin-nextjs'
1:08:14 PM: Caching artifacts
1:08:14 PM: Started saving node modules
1:08:14 PM: Finished saving node modules
1:08:14 PM: Started saving build plugins
1:08:14 PM: Finished saving build plugins
1:08:14 PM: Started saving pip cache
1:08:14 PM: Finished saving pip cache
1:08:14 PM: Started saving emacs cask dependencies
1:08:14 PM: Finished saving emacs cask dependencies
1:08:14 PM: Started saving maven dependencies
1:08:14 PM: Finished saving maven dependencies
1:08:14 PM: Started saving boot dependencies
1:08:14 PM: Finished saving boot dependencies
1:08:14 PM: Started saving rust rustup cache
1:08:14 PM: Finished saving rust rustup cache
1:08:14 PM: Started saving go dependencies
1:08:14 PM: Finished saving go dependencies
1:08:14 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
1:08:14 PM: Failing build: Failed to build site
1:08:15 PM: Finished processing build request in 31.196502612s

Have you correctly set the API token?

Hi @hrishikesh ! Thanks for your comment, I solved this issue. I had the .env file in .gitignore and it was failing to pull the static props using the API tokens etc when building the site.

1 Like

Hi @ramclean :wave:t6:,

Welcome to the forums and thanks so much for reaching back out! :netliconfetti: Glad to hear you were able to resolve your build issue. Your feedback is greatly appreciated and will help others in the future.