Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท CLI Flags

Node.js -e and -p flags

Reference for node -e (--eval) and node -p (--print): evaluate a JavaScript string from the shell, print results, pass arguments and combine with other flags.

node -e "code" (long form --eval) runs a string of JavaScript instead of a file. node -p "expression" (--print) does the same and then prints the value of the last expression, like a calculator. Both are handy for one-liners in shell scripts and for quick experiments without creating a file.

Code given with -e runs as CommonJS by default, so require() works, and the core modules are available as globals without any import (for example fs, path, os).

Terminal
node -e "console.log(process.version)"
node -p "2 ** 10"

Try it

The examples launch a child Node process through process.execPath to show exactly what the flags do.

Node.js Evaluating and printing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Modules, input and failures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

Terminal
node -e "script" [arguments]
node --eval "script" [arguments]
node -p "expression" [arguments]
node --print "expression" [arguments]
node -pe "expression"
Part Description
-e, --eval Evaluate the string and exit when the event loop is empty.
-p, --print Like -e, and print the result of the last expression with util.inspect (strings are quoted when they are nested, plain when they are the whole result).
-pe Shorthand that combines both.
--input-type=module or commonjs Choose how the string is parsed. Only valid with -e, -p or standard input.
arguments Passed to the code; process.argv[0] is the node executable and the arguments follow it.

Exit codes: 0 for success; 1 if the code throws or has a syntax error.

Usage notes

  • Shell quoting. Wrap the code in single quotes on Linux and macOS (node -p '1 + 1'). In Windows cmd.exe only double quotes work, so use single quotes inside the code (node -p "'a' + 'b'"). PowerShell has its own rules for nested quotes.
  • -p prints the value, not the output. node -p "console.log(1)" prints 1 and then undefined.
  • Top-level await needs --input-type=module.
  • Core modules without require: in CommonJS mode fs, os, path, http and the rest are available as variables. This is convenient for one-liners and does not work in files.
  • Preloading. Combine with --require or --import (see --require and --import) to set things up before the snippet runs.
  • Environment variables are read from process.env, e.g. PORT=8080 node -p "process.env.PORT".
  • Not for long programs. Anything with several lines, quotes and escapes is easier to maintain as a real file.