Next.js Discord

Discord Forum

Reading file content with `fs` but it always returns the same content as at build time

Answered
Chien Français Blanc et Noir posted this in #help-forum
Open in Discord
Chien Français Blanc et NoirOP
Im doing following:
const version = await fs.promises
        .readFile(path.join(process.cwd(), 'bin', 'version'), 'utf-8')
        .catch((error) => {
            console.error('Failed to read version file', error)
            return 'vUnknown'
        })

and it works completely fine in dev. But then when i build it, it always shows the version number of the version file at the time the project was built. How can i avoid this?

Im using NextJS 13 with the new app directory
Answered by Chien Français Blanc et Noir
i added
export const dynamic = 'force-dynamic'

to my api route since it was considered static by next, this fixed it
View full answer

3 Replies

Chien Français Blanc et NoirOP
also, doing the same with files in the temp directory works just fine
Chien Français Blanc et NoirOP
i know the problem now, the api route is statically generated
Chien Français Blanc et NoirOP
i added
export const dynamic = 'force-dynamic'

to my api route since it was considered static by next, this fixed it
Answer