Node.js Tools: How to Use CLI Commands

By: Morpheus Data

 

When writing applications using Node.js, it is sometimes helpful to use the command line interface (CLI) to run specific commands in order to quickly find out useful information or debug various bits of code. While Node.js is often used to spin up web servers for various web applications, it can also be used to write command line applications. When testing a command line application, it can be quite convenient to simply use the console to type in a CLI command to help debug while you are already in the console environment!

Viewing all commands

If you want to take a look at all of the available CLI commands, you can use the –help (or -h for short) flag in order to get the Node.js CLI help documentation. It will display a listing of the available commands for your version of Node.js. Here is an example:

 

>node –help

>Usage: node [options] [ -e script | script.js ] [arguments]

node debug script.js [arguments]

 

Options:

-v, –version print Node.js version

-e, –eval script evaluate script

-p, –print evaluate script and print result

-c, –check syntax check script without executing

-i, –interactive always enter the REPL even if stdin

does not appear to be a terminal

-r, –require module to preload (option can be repeated)

–no-deprecation silence deprecation warnings

–trace-deprecation show stack traces on deprecations

–throw-deprecation throw an exception anytime a deprecated function is used

–no-warnings silence all process warnings

–trace-warnings show stack traces on process warnings

–trace-sync-io show stack trace when use of sync IO

is detected after the first tick

–track-heap-objects track heap object allocations for heap snapshots

–prof-process process v8 profiler output generated

using –prof

–zero-fill-buffers automatically zero-fill all newly allocated

Buffer and SlowBuffer instances

–v8-options print v8 command line options

–v8-pool-size=num set v8’s thread pool size

–tls-cipher-list=val use an alternative default TLS cipher list

–openssl-config=path load OpenSSL configuration file from the

specified path

–icu-data-dir=dir set ICU data load path to dir

(overrides NODE_ICU_DATA)

–preserve-symlinks preserve symbolic links when resolving

and caching modules.

 

Environment variables:

NODE_PATH ‘;’-separated list of directories

prefixed to the module search path.

NODE_DISABLE_COLORS set to 1 to disable colors in the REPL

NODE_ICU_DATA data path for ICU (Intl object) data

NODE_REPL_HISTORY path to the persistent REPL history file

 

Documentation can be found at https://nodejs.org/

Getting the Node.js version

If you simply need to know what version of Node.js you have installed, you can use the –version (-v for short) flag. This information is helpful to determine if you have the right version of Node.js to use a particular library or code construct, or to simply ensure you have the right version of Node.js installed for what you need to do.

Here is an example:

>node –version

v6.9.4

 

Evaluate inline code

If you want to evaluate a quick expression inline, you can simply use the –print (-p for short) flag. It will evaluate the expression and print the result to the console. Here is an example:

> node –print 4-3

1

Note that you can use –eval (-e for short) as well, but the result will not be printed to the console unless you include a console.log() around the expression.

Evaluate code in a file

You may want to evaluate the code in a file without actually running the code. In such cases, you can use the –check (-c for short) flag. For example, suppose you have a file named app.js with the following contents:

var message = ‘Hello there!’;

console.log(‘This is a new command line app.’);

console.log(message + ‘ Enjoy the app);

 

If you want to check for syntax errors, you can now use the –check flag to test the code without trying to run it, as in the following example:

> node –check app.js

C:my-appapp.js:3

console.log(message + ‘ Enjoy the app);

^^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token

at startup (bootstrap_node.js:144:11)

at bootstrap_node.js:509:3

 

The checker immediately notices the missing closing quote in the console.log on line 3. You can now fix the error and run the same command again to ensure that the fix indeed worked. If so, you will simply get an empty command prompt once it completes.

As you can see, these CLI commands can be a quick and handy way to get information or help you to debug your apps. Knowing these can save you some time as you are working on your Node.js applications, allowing you to do a little bit more work while you are still in your console!