Don't prebuild module at compile time (For reading env once, at runtime) #70644
Answered
by
GabenGar
bbergeron-xlkey
asked this question in
Help
-
SummaryMy authentication module reads from env at it's topmost level and throw an exception if certain variables are undefined. This works with other architectures, but fails with Next.js since the module is evaluated at build time, when these variables aren't defined yet. My first idea is to somehow tell Next.js to not prebuild this module (it doesn't contains any JSX anyway) so I can keep the same logic as I would have within our previous architecture. How can I do that? Any other suggestions? Example// authentication.ts
import { NextRequest } from "next/server";
import jwt from "jsonwebtoken";
const jwtSecret = process.env.JWT_SECRET;
if (jwtSecret === undefined)
// Should only crash the app at runtime, but is currently crashing at build time
throw new Error('process.env.JWT_SECRET is undefined');
export const verifyToken =
(req: NextRequest) =>
{
const cookie = res.cookies.get('session-token')!.value;
const json = jwt.verify(token, jwtSecret);
return JSON.parse(json);
} |
Beta Was this translation helpful? Give feedback.
Answered by
GabenGar
Oct 1, 2024
Replies: 1 comment 3 replies
-
Validate in the startup function instead. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
bbergeron-xlkey
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Validate in the startup function instead.