- [Instructor] You've probably noticed that up until now,
we've had to manually restart our server
every time we make a change to our code,
in order for those changes to take effect.
And wouldn't it be nice
if our server could sense when our files changed
and automatically restart itself?
Well as a matter of fact,
there's an npm package that does this for us,
and it's called Nodemon.
And just note that there are different pronunciations
for this name,
but I usually call it Nodemon.
So, let's install this package by running npm install
and this is going to be a dev dependency here.
So, we're going to say
"save dev nodemon,"
and we're going to hit Enter.
And once that installs,
all we have to do is change the command
that we use to run our server.
So, instead of running
"npx babel-node src/server.js"
like we've been doing
we're going to run,
"npx nodemon --exec"
And then the original command that we've been running
"npx babel-node src/server .js"
And if we hit Enter now,
we'll see that it'll start up our server.
And what we can do now is we can make changes
to any of the routes we want.
So for example
we could have this say something like
"Server is listening on port 8,000"
with some exclamation points after it.
And we would see
that it automatically senses when our files change
and restarts the server accordingly.
So that makes our lives a lot easier
but it's still going to be a little difficult to remember this
long command when we need to run it.
So what we can do is we can actually create a sort
of shortcut script
in the package .json file
that will run this command for us.
So let's open up package .json in our backend folder
and down here in the script section,
right above this test script here
we're going to add another script.
And this one, we're just going to call dev
all right, this is going to be the development script.
If you will.
And inside here
we're going to put that command that we just ran.
So "npx nodemon --exec
"npx babel-node src/server.js"
And let's save that file.
And now what we can do
now that we have this dev script here
is instead of having to type this whole thing
into the terminal every time we want to run it,
we can simply type npm run.
And then the name of the script that we just created.
So npm run dev
and that'll take care of running that long command for us.
And one more thing
let's just remove the exclamation points from there
and we'll see that updates automatically for us.