- Scripting in a package.json file
gives us a way to do a simple command,
to repeat, or execute complex commands.
If you have a little bit of experience
developing applications, you've seen this in action.
There are so many of them that it's better to show you
where you can reference them first.
So go to docs.npmjs.com/misc/scripts.
And as you scroll here, you're gonna see
all the pre-defined properties that you can use
inside of your scripts.
So you have publish, start, version,
pre-test, build, and many more.
And you can also create your own,
which I'm gonna show you in a few seconds.
The ones you see here are available by default with npm.
So if we go back to VS Code and look at the lines
six through eight on the package.json file,
you're gonna see a script section with a test script
that's already been in there.
And that's usually a default script
that's put in there when you do npm init.
So how it works, you add a script property,
so in this case test, and then the value is the command
that's gonna run when you run that script here.
So let me demonstrate how we can change and work with this.
So the first thing I'm gonna do before I actually
create a new script, I'm going to install nodemon,
which is a utility to restart a node server
whenever you've made changes to the code.
Now let's create the script.
So what I'm gonna do here is you raise that whole line,
and then I'll create a new script.
And as I start typing, you're gonna see that
it already suggested a few things that you can use,
and these are all connected to what we see here.
So these are all the predefined scripts
or properties that you can use, so let's use start.
And then when we do the start script,
this is the code that we're gonna run.
So nodemon will run the index.js,
but make sure that it uses babble first
to convert the exec code into readable code for the web.
And this is the rest of what we need to do.
And basically then all I have to do is do npm start,
but make sure you save your package.json file first,
and then do an npm start.
So when I run this, it's as if I did this code
in my command line.
So if I did this, it's exactly the same thing
as if I did-- let me just remove the start here.
So if I do this, it's exactly the same as if I do npm start,
and it's gonna run the exact same command.
So what I'm doing here is doing this same command
with less code.
Now let's just clear that.
So when I do npm start, it's going to run this command here,
and then start my server.
So this is if we use the presets
that comes with node, or npm.
But if I do something, I'd like to do something different
or create my own, you can literally do the same thing here,
so let's copy that line,
and paste it there, and call this nodemonthis.
This is really not a preset that exists,
and it's gonna run the same command.
So the way to run your own commands
is to do npm run nodemonthis,
and again, I wanna make sure that this is available,
so I have to save this here.
And then I can run npm run nodemonthis,
and it runs the exact same command here.
So as you can see, it's really easy to add scripts,
or even your own scripts, to actually run multiple scripts
or multiple commands inside of your application.