Next.js Discord

Discord Forum

How to execute when NextJS app is started?

Answered
Cuvier’s Dwarf Caiman posted this in #help-forum
Open in Discord
Cuvier’s Dwarf CaimanOP
How can I make NextJS run first-time code, e.g. create database if it does not exist and populate with fetcher? I understand middleware isn't the one I would be looking at, it is something that is being done every time a request is done from user e.g. visiting page, but what about code level?
Answered by linesofcode
this is typicaly something done duing build and deploy time
View full answer

11 Replies

this is typicaly something done duing build and deploy time
Answer
not during run-time
in your package.json
override the "build" script
to do something like
"build": "sh ./setup-db.sh && next build"
look up migrations
which is the keyword you're looking for
when it comes to setting up dbs and populating with data
Cuvier’s Dwarf CaimanOP
Thanks @linesofcode for putting me into the right direction
Original message was deleted
Giant panda
This is not only wrong, but also a blatant ChatGPT copy/paste. Please refrain from doing this.