`process.cwd()` being cached at build time.
Unanswered
English Angora posted this in #help-forum
English AngoraOP
I'm trying to use
How do I go about debugging this, and getting the right information?
process.cwd() to get my application's run directory, in a standalone build on 15.5. The application is built on one machine, then the static assets and .next directory are packaged up and moved to the server that will be running it. When the standalone service runs, and prints its process.cwd() to a page element for debugging, the path is from the build server, not where it's running.How do I go about debugging this, and getting the right information?
56 Replies
English AngoraOP
It does appear that process.* is cached/optimized/compiled during the standalone build. This is a problem, because prcoess.cwd() is then a lie.
English AngoraOP
This is causing me problems, because it's being built in a very different location than it's being run, and as a result, it can't add a subdirectory and file to the cwd(), to find data on disk when running.
I would appreciate help finding a solution to these problems.
I would appreciate help finding a solution to these problems.
English AngoraOP
My investigations so far appear to lead down 2 paths:
1) Build in place. Ew.
2) Docker.
Docker also has a bunch of overhead for me. I have chosen to work around things.
That said, the problem still stands. What is the expected method for self-hosting?
1) Build in place. Ew.
2) Docker.
Docker also has a bunch of overhead for me. I have chosen to work around things.
That said, the problem still stands. What is the expected method for self-hosting?
Morelet’s Crocodile
This is largely dependent on where
process.cwd() is being called in your app. For example, Next will do whatever it takes to make route handlers and server components static by pre-rendering them at build time. This includes modules which are only imported/called by route handlers/server components. You can override this: https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-configEnglish AngoraOP
That might explain it. This is an issue, specifically, with my database.ts which lives under /app/lib, and is called to try to get the database connection. I will read that page and see what I can see.
For reference: https://kysely.dev/docs/getting-started?dialect=sqlite I was following this, and it's under "instantiation". It's not a part of a component, it's an "outside of React" section.
@alfonsüs ardani where are you calling your process.cwd?
English AngoraOP
const dialect = new SqliteDialect({
database: new SQLite(path.join(process.cwd(), env.DATABASE_FILE)),
})English AngoraOP
export const db = new Kysely<Database>({
dialect,
})This is then used as the database handle in route handlers and components.
@alfonsüs ardani ok now can i see the build logs?
English AngoraOP
What are you hoping to see?
@English Angora What are you hoping to see?
whether if the path is dynamic or static
denoted by
f or o@alfonsüs ardani whether if the path is dynamic or static
English AngoraOP
├ ƒ /apikeys/update 147 B 115 kB
i see, yeah i can't figure it out
English AngoraOP
Well, thank you for trying. I do appreciate that.
I think this is a known, but little-talked-about thing, that's worked around by the Docker build process.
idk for sure but its weird if its the case that Next.js statically replaces
process.cwd() into a static string during build process (even if the route turns out to be dynamic)English AngoraOP
I think it's an overly-eager optimization. It gets called once, but it isn't marked as dynamic, somehow.
what doesn't get marked as dynamic?
English AngoraOP
the cwd() call.
i dont think that matters since you already have your route marked as dynamic
fso the function should run at runtime, not at build time
im curious: have you tried calling process.cwd() directly inside the route? (not outside)
English AngoraOP
i have not.
allright, its fine
English AngoraOP
Hm. Actually, I am a little confused by a route marked as static. The entire page function is:
I don't understand why that's marked as static.
export default async function Page() {
const data = await getDataFromDB()
return <ClientComponentToRenderData data={data} />;
}I don't understand why that's marked as static.
English AngoraOP
that's a different page.
it probably wasn't using any dynamic API
try adding
await cookies() or await connection() (idk if your version already have connection)English AngoraOP
it's awaiting data from the database. Not headers, I admit, but it's using the await keyword, which should be enough.
no its not enough
since you can await stuff for static builds...
how are you going to fetch page data statically if a simple
await would turn static page to dynamic?English AngoraOP
so i should
export const dynamic = 'force-dynamic'well i dont really recommend that
its an escape hatch that should be used sparingly
especially if this can be solved with simple
await connection()English AngoraOP
I had not heard of connection(). I assumed it was introduced in 16. I'm using 15 and it's present. You're right, that's what I should use.
can you check real quick if there is
await conenction() in your version?English AngoraOP
I checked the docs...
vscode seems to thing it is - it found the import.
ok so all good?
English AngoraOP
for this route, yes. I think I'm good, for the moment.
Thank you, again.
i am not certain that's the solution. The cwd issue still remains, and I've worked around it by simply not using "./" and having the bad substitution happen. Using an environment variable would also work, and using a Docker multi-stage build that drops the end result into the same base system as the build system also works around it.
I just don't have a good Docker setup here, so ... that way's not good for me right now.
I see. At which route does the cwd issue occurs?
it seems like im missing critical informations that would otherwise be present in the original post
English AngoraOP
Okay, let me write it as a fresh start. One moment.
English AngoraOP
I have a project using Kyseley to integrate with the database. In this project I have an
Dynamic routes and components in the project make use of this
The issue is that when
app/lib/database.ts which has the following call:const dialect = new SqliteDialect({
database: new SQLite(path.join(process.cwd(), AppConfig.database.path)),
})
export const db = new Kysely<Database>({
dialect,
})Dynamic routes and components in the project make use of this
db as a standard Kyseley interface.The issue is that when
AppConfig.database.path is ./database.sqlite, this compiles to a path that resolves on the build server, but does not when moved to the server it will run on. The implication is that process.cwd() was cached as a static value during the compile step.this is unrelated and unideal but in my setup, i made a wrapper run.ts script that sets the cwd to the env variable.