In addition to letting you write command-line JavaScript programs, Node gives you a way to orchestrate your workflow as you develop these programs. It is a powerful feature that you should take advantage of. To see how this works, you will add a bit of automation to your project.
Take running your server, for example. Every time you want to try something new in your code, you have to repeat a few steps:
make the change to the code in your editor
switch to your terminal
press Control-C to stop the program
run npm start
to start your program again
You could write a program to automate the work of restarting your
service. You are in luck, though – someone has already written it
for you, in a module called nodemon
. Integrating
nodemon
into your workflow early on will make
writing your program a much smoother experience.
In the terminal, stop your program and run the following command to install the nodemon
module:
npm install --save-dev nodemon
You will see the lines below, in which npm
is warning you
about some blank fields in your package.json file.
Do not be alarmed – just be aware that npm
is a stickler for details.
npm WARN [email protected] No description npm WARN [email protected] No repository field.
Notice the --save-dev
option that you used in this npm install
command.
It tells npm
to help you keep a list
of any third-party modules your application depends on.
That list is stored in your package.json
file.
If necessary, all of the dependencies in that list
can be installed by running the npm install
command (with no arguments).
This means that when you are sharing your code you do not need to include
all of the third-party modules as well.
If you look in your
package.json file, you will see that npm
created
a new "devDependencies"
section for you, with an entry for nodemon
.
... "author": "", "license": "ISC", "devDependencies": { "nodemon": "^1.9.1" }
Now, update your package.json to add another item
to the "scripts"
section:
... "scripts": { "test": "echo "Error: no test specified" && exit 1", "start": "node index.js", "dev": "nodemon index.js" }, ...
In the terminal, restart your node program via your new npm
script using the command npm run dev
. Note that the command is
not simply npm dev
. This differs from npm start
in that npm
assumes that certain commands (like start
) will exist.
For custom npm
scripts, you must specify that you want to run
them.
You should see that nodemon
is now managing
your node program (Figure 15.9).
In index.js, change "Hello, World"
to
"Hello, World!!"
and save the
changes. nodemon
notices and restarts your node
program automatically (Figure 15.10).
As you continue to work with node and npm
in
the following chapters, you will
periodically pull in a new module to help out.
3.142.249.42