[next-auth]/route.ts is executed during build
Unanswered
Asian black bear posted this in #help-forum
Asian black bearOP
Following is my
Build logs (see
[next-auth]/route.ts code and it is getting executed during the build step.import NextAuth, { NextAuthOptions } from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { MongoDBAdapter } from "@auth/mongodb-adapter";
//import getClientPromise from "@/app/lib/mongodb";
console.log("Next auth executed")
export const authOptions: NextAuthOptions = {
debug: true,
providers: [
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
callbacks: {
session: async ({ session, token, user }: any) => {
if (session?.user) {
session.user.id = user.id;
}
return session;
},
},
//adapter: MongoDBAdapter(getClientPromise()),
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };Build logs (see
Next auth executed being printed).> next build
- info Creating an optimized production build
- info Compiled successfully
info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
- info Linting and checking validity of types
- info Collecting page data ..Next auth executed
Next auth executed
Next auth executed
Next auth executed
- info Collecting page data
- info Generating static pages (7/7)
- info Finalizing page optimization
Route (app) Size First Load JS
[Redacted]
λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
â—‹ (Static) automatically rendered as static HTML (uses no initial props)34 Replies
[next-auth] have the λ symbol at the start? as otherwise it could be just to look at the file (not prerendering it tho)dynamic routes will always be dynamic unless you use generateStaticParams
I dont see you getting any errors here, so what's the issue?
@Asian black bear Following is my `[next-auth]/route.ts` code and it is getting executed during the `build` step.
import NextAuth, { NextAuthOptions } from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { MongoDBAdapter } from "@auth/mongodb-adapter";
//import getClientPromise from "@/app/lib/mongodb";
console.log("Next auth executed")
export const authOptions: NextAuthOptions = {
debug: true,
providers: [
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
callbacks: {
session: async ({ session, token, user }: any) => {
if (session?.user) {
session.user.id = user.id;
}
return session;
},
},
//adapter: MongoDBAdapter(getClientPromise()),
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
Build logs (see `Next auth executed` being printed).
> next build
- info Creating an optimized production build
- info Compiled successfully
info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
- info Linting and checking validity of types
- info Collecting page data ..Next auth executed
Next auth executed
Next auth executed
Next auth executed
- info Collecting page data
- info Generating static pages (7/7)
- info Finalizing page optimization
Route (app) Size First Load JS
[Redacted]
λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
â—‹ (Static) automatically rendered as static HTML (uses no initial props)
i think this is a nextjs bug, related: https://github.com/vercel/next.js/issues/55863
try adding
that said, i don't see any issues here other than the build being a bit slower
try adding
export const revalidate = 0 there, then it should not be run during buildthat said, i don't see any issues here other than the build being a bit slower
but they are adding console log outside the function, of cause that would run..? (like how else would it know about the revalidate config)
@riský but they are adding console log outside the function, of cause that would run..? (like how else would it know about the revalidate config)
configs such as revalidate must be statically analysable
nextjs takes the value as string then parse it
o.O wait they don't import
it doesn't execute the page
sorry then
now that makes so much more sense...
@Clown I dont see you getting any errors here, so what's the issue?
Asian black bearOP
The issue I am getting is in the mongodb driver code which you see as commented here.
The driver code is expecting an environment variable to be defined hence it throws an error.
The driver code is expecting an environment variable to be defined hence it throws an error.
try what joulev suggested and see if the error continues
Yeah its a bug. Guess you'll have to follow up with that or downgrade
you should add the env var during build
@joulev env var should be defined during build too
Asian black bearOP
Even for the dynamic apps? I don't want to hardcode my DB credentials.
while this may be a nextjs bug, that bug by itself is mostly harmless
@Asian black bear Even for the dynamic apps? I don't want to hardcode my DB credentials.
from my experience, env var being added dynamically may not behave reliably, but env var present during build will work normally
this is just a mongodb connection string, it's not like it changes everyday
@Asian black bear Even for the dynamic apps? I don't want to hardcode my DB credentials.
I don't want to hardcode my DB credentials.just add the db connection string to an env file? .env or .env.local or similar
you don't need to (and shouldn't) put it inside your code
@joulev you don't need to (and shouldn't) put it inside your code
Asian black bearOP
I have a code block which checks if the env variable is not defined then throw the error.
So if I add a dummy string to .env file, this check will always pass hence making it useless, no?
So if I add a dummy string to .env file, this check will always pass hence making it useless, no?
@Asian black bear I have a code block which checks if the env variable is not defined then throw the error.
So if I add a dummy string to .env file, this check will always pass hence making it useless, no?
why don't you use the connection string? why do you use a dummy string here?
Why would you add a dummy string?
Asian black bearOP
Actually I deploy my code using Docker hence I am writing a docker file so the db connection string will vary depending upon the environment.
Can docker not differentiate between your environments?
As long as the key is same you should be able to just swap the values around for different environment.
Asian black bearOP
So you think it makes sense to have this check in my MongoDB driver code? As the mongodb string will always be defined per the suggested answers.
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}I mean, nothing wrong with keeping the test.
Tho im pretty sure mongodb will complain anyways if the environment variable wasn't present so..
Asian black bearOP
But it does not complain during the build step. I mean I have always used
pages so I have never witnessed this happening. So this behaviour might be specific to the app router.