Lesson 16
Using nodemon to Restart the Server Every Time We Make a Change – Nodejs Express
Summary
Lesson Summary: Using Nodemon for Automatic Server Restart
In this lesson, we learned how to use the third-party package Nodemon to automatically restart our server whenever we make changes to the source code. Here's a concise summary of the steps we followed:
Steps to Set Up Nodemon
-
Understanding the Need for Nodemon:
- When you change
server.js
, you typically have to manually stop and restart the server. - Nodemon automates this process.
- When you change
-
Installing Nodemon:
- Global Installation: You can install Nodemon globally with the command:
However, this isn't ideal as it assumes the user has Nodemon installed globally.npm install -g nodemon
- Local Installation: Preferably, install it locally as a development dependency:
npm install --save-dev nodemon
- This adds Nodemon to your project's
devDependencies
inpackage.json
.
- Global Installation: You can install Nodemon globally with the command:
-
Running Your Server with Nodemon:
- Use
npx
to run the locally installed Nodemon:npx nodemon server.js
- Use
-
Automatic Restart on File Change:
- After making changes to
server.js
and saving, Nodemon automatically restarts the server.
- After making changes to
-
Creating NPM Scripts:
- You can simplify the command by creating a script in
package.json
:"scripts": { "dev": "nodemon server.js", "start": "node server.js" }
- Now, you can start your server with:
npm run dev
- The
npm start
command runs your server without Nodemon for production use.
- You can simplify the command by creating a script in
Conclusion
By using Nodemon and defining scripts in your package.json
, you can streamline your development workflow by automating the server restart process, allowing for a more efficient coding experience.
Video Transcript
In this lesson, we are going to learn how to use the third-party package called an old
mon to restart our server every time we make a change to the source code.
Let's get started.
You may have noticed that every time I make a change to the file server.js, I have to
kill my server that I can run with node server.js and restart it, that is, rerun the same script
again.
It would be nice if this process could be automatic.
To do that, we use nodemon.
So first, we have to install that npm package.
There are two ways you can install nodemon.
You can do it globally or you could do it locally to your project.
I prefer locally to the project so whoever downloads your project will also install the
nodemon dependency so they can run the command to develop your app easily and nodemon can
automatically be fetched and executed.
I'm going to show you the first way is you do npm install-g for global nodemon.
If you do it this way, it will be installed globally and you can just run nodemon by saying
nodemon and whatever script, in this case, nodemonserver.js.
That is nice but it assumes that you already have nodemon installed globally.
If somebody were to download your project and they tried to do this very command, it probably
wouldn't work if they did not have nodemon already installed globally.
So that process is not automatic for them to install it.
So that's why I prefer installing the package locally as a dev dependency.
So let's do npm install-saved-dev.
That means this is a development dependency that's not really necessary for production
but it's necessary for our development of the project.
Space nodemon.
Enter it and then let's wait for it to install that module.
You can go back to the text editor and inspect your package.json file and you're going to
see that it's going to be...
There is going to appear a new entry here called dev dependencies with nodemon added
right here.
Nodemon in this case I'm using 204.
So let's see.
Now that we have nodemon installed in our project dev dependency, it should be under
the nodemodules directory slash nodemon.
Okay, right here.
Now if I wanted to execute nodemon from the local install here, I could say nodemodules
dot slash nodemon slash nodemon, etc.
There is a way to avoid doing this and that's using the npx command.
If you do npx nodemon, what's going to do is use the nodemon that is installed in your
local project under your node modules.
So let's do npx nodemon server dot js.
Now server is listening on part 3000.
You can see that server dot js is running.
Now let's make a change to server dot js here.
Let's change the first text to be hello here and I'm going to save.
Watch as I save my file here.
You can see in the terminal here nodemon restarting due to changes.
So it restarted the server so I didn't have to do it manually.
I told you that I still have to refresh my browsers.
There's no so called live reload here.
I still have to go to the browser and press refresh and I see real low here for the first
entry.
Now to finish off, it would be nice if we could create what's called an npm command so that
we don't have to know the underlying implementation of the development server.
So let's kill the server here.
It would be nice if we could do the following npm run for example dev or development or
whatever name you want to give us.
So let's say npm run dev.
It would be nice if we could just save this command like this and it would automatically
start nodemon server dot js.
To do that you can see that it's a missing script because we don't have it yet.
If you want to create this go to your package dot json under the project root and you can
see there's an entry called scripts and this property that it's an object you can add a
new property here called dev and this is going to be the name that you're going to type in
the terminal.
Columns and I'm going to say nodemon server dot js.
No you don't need npx here.
Npm will automatically use the nodemon that's under node modules.
And just before I say that I want to make sure that this video uses npm version 6 14.4
in case there's discrepancies in the future as the versions get updated.
And I'm using node version 12 point 18 point zero npx 6 14 point zero usually the same
as npm.
Okay so let's test it out.
I'm going to say npm run dev and I didn't save my file sorry.
Let me save my file come back to the terminal here and you can see npm run dev it's going
to run nodemon server dot js and now my server is running.
You can also add a special command to start the server without nodemon if you want you
can say start and this one is special because you don't need to type run before.
You can say just node server dot js and that will run without restarting.
We want to use this one for production for your back end server.
So back here in the terminal I can say npm start.
No there's no run because the special one and it will do node server dot js.
This one will not restart every time you make a change if this is the one ideal for production.
Let me kill server okay nice.
So that's it for this lesson.
We learned to use nodemon to improve our development workflow that is we don't have to manually
restart the server every time we make a change to server dot js.
Nodemon will automatically do that for us.
We learned to add a new enter to npm script so we can run our desired command under a
certain alias in this case I give the alias name dev so every time I say npm run dev it's
actually running behind the scenes nodemon server dot js.
Also creating another one for npm start a special case node server dot js will not restart
and this one is the ideal for production.
Thank you so much for watching and until the next time.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: