Have you been developing a NextJS app with dynamic routing (using maybe Express), and found that every time you make a change you have to do the tedious process of shutting down the server (CTRL+C) and restarting it? (npm run dev
).
If you're used to working with NodeJS, or ExpressJS, you've probably come across nodemon. It's a utility that enables hot reloading on Node-based servers, so that whenever you make a change to a server file and save -- it instantly starts to restart without any prompt from your part.
But nodemon doesn't work out of the box with NextJS and requires a small amount of configuration. If you try running nodemon without a config or the proper CLI params, you'll find that your server will start acting real wonky. My server started restarting infinitely, because it was detecting changes each time NextJS compiled, triggering an infinite loop of compilations.
This guide assumes you have a NextJS project with dynamic routing setup. You can find a few in the examples section of the NextJS repo
The solution?
Nodemon accepts a configuration file, which allows you have a greater degree of control over the process. By adding a few values to this file, we can solve all our issues.
Install nodemon
If you haven't already, install nodemon:
npm install --save-dev nodemon
Create the config file
Create a nodemon.json
file in the project root and paste the following into it:
{
"verbose": true,
"ignore": ["node_modules", ".next"],
"watch": ["server/**/*", "server.js"],
"ext": "js json"
}
This tells nodemon to ignore the .next
folder, which is used as a cache for the Next compiler (and triggers the infinite reload). And we also tell it which file to watch for changes from. I keep my server file in a separate server folder, since I have stuff like routes/middleware/etc that need separate files and folders.
Update your npm dev script
Now you can modify your package.json
and update the 'dev' script value to use nodemon instead of the default node server.js
:
"scripts": {
"dev": "nodemon -w server/server.js server/server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
},
Now you can run npm run dev
and you'll have yourself a hot-reloading server.
I found this solution on the NextJS Github issues, where a people were having - go figure - the same issue.
Hope that helps ✌️
Ryo
References:
Top comments (8)
I am getting a ECONNREFUSED ::1:64146 error when trying to spin up my app with nodemon script:
When I spin up my app with Next script, the app spins up fine. Here is the script:
Here is my nodemon.json:
Here is my server.js:
Here is my next.config:
Here is the Error I'm getting when using nodemon:
Here is my package.json:
Any thoughts on what the issue is?
Good one.
Quick correction: you shouldn't use
-w
on your package.json or it'll ignore yournodemon.json
config.Yeah, should not it be just
nodemon server.js
to start up the main server file and it will trigger server reloadin case any of specified files in
nodemon.json
were altered?Okay great, this one is pretty standard. How to refresh the browser when you make changes though using Next/ SSR?
I'd do Ctrl+F5
Wow, thank you so much!
Thanks for sharing!
I know nodemon since my first try with node.js. That's good you're sharing it with JavaScript beginners :)