workos / authkit-nextjs

The WorkOS library for Next.js provides convenient helpers for authentication and session management using WorkOS & AuthKit with Next.js.
MIT License
68 stars 19 forks source link

Deploying to Vercel throws NoApiKeyProvideException #113

Closed sheldonvaughn closed 1 month ago

sheldonvaughn commented 1 month ago

Deploying to Vercel returning an issue:

app-name:build:    Collecting page data ...
app-name:build: NoApiKeyProvidedException: Missing API key. Pass it to the constructor or define it in the WORKOS_API_KEY environment variable.
app-name:build:     at new E (/vercel/path0/apps/app-name/.next/server/chunks/941.js:29:100117)
app-name:build:     at new h (/vercel/path0/apps/app-name/.next/server/chunks/941.js:29:39452)
app-name:build:     at 68791 (/vercel/path0/apps/app-name/.next/server/chunks/941.js:30:79751)
app-name:build:     at t (/vercel/path0/apps/app-name/.next/server/webpack-runtime.js:1:143)
app-name:build:     at 98387 (/vercel/path0/apps/app-name/.next/server/chunks/941.js:30:40890)
app-name:build:     at t (/vercel/path0/apps/app-name/.next/server/webpack-runtime.js:1:143)
app-name:build:     at 83024 (/vercel/path0/apps/app-name/.next/server/chunks/770.js:3:67080)
app-name:build:     at t (/vercel/path0/apps/app-name/.next/server/webpack-runtime.js:1:143)
app-name:build:     at 65843 (/vercel/path0/apps/app-name/.next/server/app/_not-found/page.js:1:5444)
app-name:build:     at t (/vercel/path0/apps/app-name/.next/server/webpack-runtime.js:1:143) {
app-name:build:   status: 500
app-name:build: }
app-name:build: 
app-name:build: > Build error occurred
app-name:build: Error: Failed to collect page data for /_not-found
app-name:build:     at /vercel/path0/node_modules/.pnpm/next@14.2.15_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/build/utils.js:1268:15
app-name:build:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
app-name:build:   type: 'Error'
app-name:build: }
app-name:build:  ELIFECYCLE  Command failed with exit code 1.
app-name:build: ERROR: command finished with error: command (/vercel/path0/apps/app-name) /pnpm9/node_modules/.bin/pnpm run build exited (1)
app-name#build: command (/vercel/path0/apps/app-name) /pnpm9/node_modules/.bin/pnpm run build exited (1)

.vercel/.env.development.local variables match

WORKOS_CLIENT_ID="client_..." # retrieved from the WorkOS dashboard
WORKOS_API_KEY="sk_test_..." # retrieved from the WorkOS dashboard
WORKOS_COOKIE_PASSWORD="<your password>" # generate a secure password here

Issue resolved:

This seems to occur whenthe default build command is set to turbo run build, causing the error. Replacing the build command with npm/pnpm commands seems to resolve the error.

iohcidnal commented 1 month ago

I am also getting the same error deploying in Vercel.

sheldonvaughn commented 1 month ago

Hey @iohcidnal - we were able to resolve this issue by ensuring the environment variables were populated as follows and that npm build or pnpm build are being used as the build command in Vercel:

image

Hoping that helps resolve this for you but keep us posted!

iohcidnal commented 1 month ago

Thanks @sheldonvaughn! I already have my env vars declared this way in Vercel, but I still get the error. I am also using turbo repo.

iohcidnal commented 1 month ago

I think I figured out what's going on. Since I'm using turbo repo, the default build command was set to turbo run build, causing the error. Replacing the build command with npm run build seemed to work. The confusing part is running turbo run build locally worked but not in Vercel.

sheldonvaughn commented 1 month ago

Thanks @iohcidnal - I'll add that turbo repo and the turbo run build seem to be part of the issue here and close the issue. Hoping this helps with any future finders of this error - thanks for your detailed response!

kittenkill commented 1 week ago

Hi!

Is there a way to defer the definition WorkOS's env variable at runtime?

Currently, its forcing me to define the env's at build time, so when buildling a container, the env var is backed into it, with no option to change its values.