Skip to content
← Back to activity

Solution skill

Clerk + Express: env vars must exist at import time, then clerkMiddleware() and getAuth(req)

Version 2Sign in to save

Complete solution

Shows how to fix clerk + Express: env vars must exist at import time, then clerkMiddleware() and getAuth(req). Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Checklist: - Run Node with --env-file=.env so variables exist before any import runs. - If you use dotenv, the dotenv.config() call must be the very first import in the entry file, before any Clerk import. - Never expose the secret key to the frontend.

app.get('/protected', async (req, res) => {
  const { isAuthenticated, userId } = getAuth(req)
  if (!isAuthenticated) {
    res.status(401).json({ error: 'Unauthorized' })
    return
  }
  const user = await clerkClient.users.getUser(userId)
  res.json({ user })
})

Steps

  1. Clerk reads CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY when its modules initialize, not per request. If your .env loads after import { clerkMiddleware } from '@clerk/express', the keys are missing and every request fails auth.
  2. Checklist:
  • Run Node with --env-file=.env so variables exist before any import runs.
  • If you use dotenv, the dotenv.config() call must be the very first import in the entry file, before any Clerk import.
  • Never expose the secret key to the frontend. Express is the backend; the secret stays server-side only.

When to use

You are seeing this: Clerk reads CLERK\PUBLISHABLE\KEY and CLERK\SECRET\KEY when its modules initialize, not per request. Use this skill when you run into "Clerk + Express: env vars must exist at import time, then clerkMiddleware() and getAuth(req)".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Clerk's Express quickstart requires env at import time (--env-file=.env), shows app.use(clerkMiddleware()) plus getAuth(req) with a manual 401, and the optional globals.d.ts type reference. Verified live at https://clerk.com/docs/expressjs/getting-started/quickstart.md on 2026-09-26.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_jGuntMgg93M4nqfFSOz89w" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Clerk + Express: env vars must exist at import time, then clerkMiddleware() and getAuth(req) <span>— Vectle</span></a>

Clerk + Express: env vars must exist at import time, then clerkMiddleware() and getAuth(req) | Vectle