Immutable solution version
Clerk + Fastify: clerkPlugin() registration, ESM, and getAuth(request)
Complete solution
Shows how to fix clerk + Fastify: clerkPlugin() registration, ESM, and getAuth(request). Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Run with node --env-file=.env, or load your env loader before importing @clerk/fastify.
import Fastify from 'fastify'
import { clerkClient, clerkPlugin, getAuth } from '@clerk/fastify'
const fastify = Fastify({ logger: true })
fastify.register(clerkPlugin)
fastify.get('/protected', async (request, reply) => {
const { isAuthenticated, userId } = getAuth(request)
if (!isAuthenticated) {
return reply.code(401).send({ error: 'User not authenticated' })
}
const user = await clerkClient.users.getUser(userId)
return reply.send({ message: 'ok', user })
})Steps
- The Clerk Fastify guide assumes ECMAScript modules:
"type": "module"in package.json. Mixingrequire()with the ESM-only imports is the first failure. - Env vars (
CLERK_PUBLISHABLE_KEY,CLERK_SECRET_KEY) must be available before any Clerk module initializes. Run withnode --env-file=.env, or load your env loader before importing@clerk/fastify. Same rule as Express: Clerk reads keys at import time. - -
fastify.register(clerkPlugin)with no call parens: it is a plugin function, not middleware factory output.
- TypeScript:
getAuth(request)is typed against the Fastify request; do not pass a raw Node req. - As with Express,
clerkClient.users.getUser()is a Backend API call per invocation; cache or avoid in hot paths.
When to use
You are seeing this: The Clerk Fastify guide assumes ECMAScript modules: "type": "module" in package.json. Use this skill when you run into "Clerk + Fastify: clerkPlugin() registration, ESM, and getAuth(request)".
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 Fastify quickstart requires ESM and env before Clerk imports, registers clerkPlugin for all routes, and protects routes with getAuth(request) plus a manual 401. Verified live at https://clerk.com/docs/fastify/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_yLmJ8kNERYvVNjay-RdJ_w" 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 + Fastify: clerkPlugin() registration, ESM, and getAuth(request) <span>— Vectle</span></a>