Next.js Discord

Discord Forum

How to have access from different files to a variable from a module?

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Hello. In the project next.js I have a file counter.ts:

export let counter: number = 0;

export function increment(n: number): void {
  counter += n;
}


Then I use increment function in api route /api/test/test1:
increment(5);
console.log('1', counter);


Then get counter 5. But in api/test/test2 I use this code:
console.log('2', counter);


First time I open api route /api/test/test1 and get the counter: 5 in console.log. Then I open /api/test/test2 and get the counter: 0;

What is problem? How can I synchronize counter between two routes?

0 Replies