Importing doesn't work the way I expect?
Answered
Tomistoma posted this in #help-forum

TomistomaOP
Hello,
when importing an object from a file located at @/lib/{file.ts} on the App Router (Next.js 15.1.6, dev environment, turbopack), the changes to the object are not synced across all imports, i.e. if I add something to the object it is not reflected in other files.
Let's say the files are somewhat like this:
@/lib/{file.ts}
after calling the loginUser function from API Route /login, the new user is not shown in i.e. API Route /menu
(console.log returns {}) as if it was freshly initiated
Honestly I do not know if this is intended behavior or not, but I would like some clarification and solutions if possible 😁
Using a database is not suitable for me, but I really wanna use API Routes (import has to sync).
Thanks!
when importing an object from a file located at @/lib/{file.ts} on the App Router (Next.js 15.1.6, dev environment, turbopack), the changes to the object are not synced across all imports, i.e. if I add something to the object it is not reflected in other files.
Let's say the files are somewhat like this:
@/lib/{file.ts}
const users: { [index: string]: User } = {};
async function loginUser(username: string, password: string) {
const user = new User(username, password);
const loginStatus = await user.login();
if (loginStatus) {
users[username] = user;
}
return loginStatus;
}
after calling the loginUser function from API Route /login, the new user is not shown in i.e. API Route /menu
(console.log returns {}) as if it was freshly initiated
Honestly I do not know if this is intended behavior or not, but I would like some clarification and solutions if possible 😁
Using a database is not suitable for me, but I really wanna use API Routes (import has to sync).
Thanks!
Answered by joulev
Well, there is unfortunately no way to guarantee different pages and api routes share the same js runtime. So you either have to make a separate service to consolidate the initiation of that object, or optimise it so it no longer takes a significant amount of time to initiate.
8 Replies

@Tomistoma Hello,
when importing an object from a file located at @/lib/{file.ts} on the App Router (Next.js 15.1.6, dev environment, turbopack), the changes to the object are not synced across all imports, i.e. if I add something to the object it is not reflected in other files.
Let's say the files are somewhat like this:
@/lib/{file.ts}
javascript
const users: { [index: string]: User } = {};
async function loginUser(username: string, password: string) {
const user = new User(username, password);
const loginStatus = await user.login();
if (loginStatus) {
users[username] = user;
}
return loginStatus;
}
after calling the loginUser function from API Route /login, the new user is not shown in i.e. API Route /menu
(console.log returns {}) as if it was freshly initiated
Honestly I do not know if this is intended behavior or not, but I would like some clarification and solutions if possible 😁
Using a database is not suitable for me, but I really wanna use API Routes (import has to sync).
Thanks!

why do you want to do this?
depending on the hosting platform, very likely each api route/page runs on an independent scope and hence cannot share data to each other.
depending on the hosting platform, very likely each api route/page runs on an independent scope and hence cannot share data to each other.

@joulev why do you want to do this?
depending on the hosting platform, very likely each api route/page runs on an independent scope and hence cannot share data to each other.

TomistomaOP
well these user objects will be used to interact with another service for which i have to be logged in, and there will be some data that will keep changing in these objects. i want these changes to be reflected in all api routes, not that when i query the /settings endpoint to return different data than /menu or whatever.
is there really no way to turn off this behavior? i understand next.js is meant to be ran in vercel's serverless environment, but i intend to host it on my own hardware, where i do not have these serverless limitations
is there really no way to turn off this behavior? i understand next.js is meant to be ran in vercel's serverless environment, but i intend to host it on my own hardware, where i do not have these serverless limitations

@Tomistoma well these user objects will be used to interact with another service for which i have to be logged in, and there will be some data that will keep changing in these objects. i want these changes to be reflected in all api routes, not that when i query the /settings endpoint to return different data than /menu or whatever.
is there really no way to turn off this behavior? i understand next.js is meant to be ran in vercel's serverless environment, but i intend to host it on my own hardware, where i do not have these serverless limitations

Persistent data should be stored in persistent storage, which is a database.
Any reasons you don’t want to use a database? Since you are self hosting, setting up a db is trivial.
Any reasons you don’t want to use a database? Since you are self hosting, setting up a db is trivial.

if you don't want another service just use sqlite

TomistomaOP
awh alright then, not that i have any gripes with databases, but these objects would not need to be stored between server restarts, and this would keep the complexity down.

TomistomaOP
but on the other hand, i did use a library sometime ago that took a couple hundred ms to instantiate an object because of some connections and whatnot, and instantiating such an object on every route call may just not be feasible in that case. im just asking this for maybe some problems i may encounter in the future, if i should even use the api routes before i go too far in (and if there really is no other solution)

Well, there is unfortunately no way to guarantee different pages and api routes share the same js runtime. So you either have to make a separate service to consolidate the initiation of that object, or optimise it so it no longer takes a significant amount of time to initiate.
Answer

TomistomaOP
alright, thank you very much for your time, have already begun migrating to a separate backend without these limitations. good to know for the future, new lesson learnt 😄