NodeJS – Interacting on CMD Line / Shell – WITHOUT Creating a JS File

  • report
    Click for Disclaimer
    This Post is over a year old (first published about 4 years ago). As such, please keep in mind that some of the information may no longer be accurate, best practice, or a reflection of how I would approach the same thing today.
  • infoFull Post Details
    info_outlineClick for Full Post Details
    Date Posted:
    Mar. 18, 2019
    Last Updated:
    Nov. 11, 2019
  • classTags
    classClick for Tags

I was having trouble finding this info in one spot, so I made this post as “refresher” to myself on some easy ways to interact with NodeJS from the command line, and without needing to have an actual JS file as the input. The main reason why I wanted to figure out how to do this, was so I could check my NodeJS ABI version with a single command from my CLI

As usual, one of the best sources of info is the official docs. Here is the documentation page for the Node command line interface (CLI).

The built in “REPL”

REPL, which is an acronym for “Read-Eval-Print-Loop”, is a feature baked into the Node.js standard installation. From your command line, simply type “node” and press enter without anything after it. You will enter the REPL, in which you can type out JavaScript and instantly execute it with results. By default, Node will even detect when you are trying to write a multi-line statement (for example, a for loop), and will let you write it out before executing.

Alternative, you can also type .editor and press enter, after entering the REPL, to enter the “editor mode”. This will let you write out a longer script in the console, including line breaks, and only executing it once you are ready.

For more details and a better guide, check out Flavio’s guide here –

Using “print” (node -p)

The -p (or -print) argument basically evals the string following it and prints the result. For example, to get the ABI #, we can use:

node -p "process.versions.modules"

Using “eval” (node -e)

The “eval” option takes a string input and “evals” it, which is to execute it as though it were raw code being passed in. Since we are running in the command line interface and node will exit immediately after eval’ing, if we want to see the result of the eval option, you either need to use “node -p” instead, or in your code, make sure output is going to be passed to the console. For example, this results in the same result as my node -p example:

node -e "console.log(process.versions.modules)"

Don’t forget about piping!

Just like with almost anything on the CLI, you can pipe data to node, by using the pipe operator (“|”). For example, to print “hello world” to the console:

echo "Hello World" | node -p

… Or, pipe the output of Node to something else! For example, pipe it to grep to search for a pattern:

node -p "process.versions" | grep "openssl:\s*'(?:[^\'])+" -P

Or send version info to a text file:

NodeJS - Saving Version Info to a Text File With Cmd

I got what I wanted:

Here is a single line command that will spit out the node version and ABI # to command prompt, with some line breaks around it so it is easy to read (this is the same command shown in the thumbnail for this post):

node -p "'\n\n\n\n   Node Version = ' + process.versions.node +'  ||  ' + 'Node Version ABI # = ' + process.versions.modules"

NodeJS - Getting Version and ABI Version from Command Line

Additional Info:

Getting installation paths:

Get the install path of NPM by using:

npm config get prefix

Or by using:

which npm

2 thoughts on “NodeJS – Interacting on CMD Line / Shell – WITHOUT Creating a JS File”

  1. Daniel Collier says:

    You can also use node with xargs to pass arguments to a custom function:

    echo ‘hello’ | xargs node -e ‘console.log(process.argv[1])’

    1. joshuatz says:

      Yep, good tip!

Leave a Reply

Your email address will not be published.