Chapter 2. Scripts and Modules
We used the node command briefly in Chapter 1 to explore Node’s REPL mode and execute simple scripts. In this chapter, we’ll learn how Node loads and executes scripts and modules. We’ll start by exploring more of the options, arguments, and environment variables that can be used with the node command, and learning more about what we can do in a REPL session. Then we’ll learn about the steps Node takes to load and execute a module.
Node CLI
The node command has many options that can be used to customize its behavior. It also supports arguments and environment variables to further customize what it does, and to pass data from the OS environment to Node’s process environment.
Let’s take a look. In the terminal, type the following:
$ node -h | less
This will output the help documentation for the command (one page at a time because we piped the output on the less command). I find it useful to always get familiar with the help pages for the commands I use often:
Usage: node [options] [ script.js ] [arguments] node inspect [options] [ script.js | host:port ] [arguments] Options: - script read from stdin (default if no file name is provided, interactive mode if a tty) -- indicate the end of node options --abort-on-uncaught-exception aborting instead of exiting causes a core file to be generated for analysis --build-snapshot Generate a snapshot blob when the process exits. Currently only supported in the node_mksnapshot binary. -c, --check syntax check script ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access