How to Check Node Version on the Command Line (Not the REPL)

To check your Node.js version from the command line without entering the REPL, run node -v or node --version.

Both commands output the semantic version string (for example, v20.12.0) and exit immediately, making them ideal for shell scripts and CI pipelines. According to the nodejs/node source code, this behavior is implemented through built-in CLI flags registered in the core C++ layer.

Using the -v and --version Flags

Node.js exposes two equivalent options for retrieving the runtime version from the shell. These flags are parsed before the JavaScript engine initializes, ensuring fast execution.

  • -v — The short alias defined for convenience.
  • --version — The explicit long-form flag.

Both produce identical output by printing the value of NODE_VERSION_STRING defined in src/node_version.h and then halting the process.


# Short form

$ node -v
v20.12.0

# Long form

$ node --version
v20.12.0

How It Works in the Node.js Source Code

The implementation resides in the native C++ layer of the Node.js runtime, ensuring consistent behavior across platforms.

Where the Flag Is Defined

In src/node_options.cc, the CLI parser registers --version with its -v alias at lines 1336-1338. When the argument parser encounters either token, it triggers an early exit path that prints the version string without booting the V8 isolate or entering the REPL.

This architecture guarantees that node --version executes in milliseconds, as it bypasses the heavier initialization steps required for JavaScript execution.

Where the Version String Comes From

The actual string displayed by these commands originates in src/node_version.h. This header file defines the NODE_VERSION_STRING macro, which concatenates the major, minor, and patch version numbers (e.g., "20.12.0"). The CLI flag handler reads this constant and prefixes it with a lowercase v before writing to stdout.

Practical Examples for Shell Scripts

Because the command exits with status code 0 immediately after printing, it integrates cleanly into conditional logic and environment checks.

Check for a specific major version:

if [[ "$(node -v)" == v20.* ]]; then
  echo "Running on Node.js 20"
else
  echo "Wrong Node.js version"
fi

Store the version in a variable for later use:

NODE_VERSION=$(node --version)
echo "Detected runtime: $NODE_VERSION"

Summary

  • Primary command: node -v or node --version prints the version and exits without starting the REPL.
  • Source location: The flags are registered in src/node_options.cc and read the string from src/node_version.h.
  • Exit behavior: The process terminates immediately after output, returning status 0.
  • Use in scripts: The command is safe to use in shell conditionals and variable assignments because it never enters the interactive JavaScript environment.

Frequently Asked Questions

What's the difference between node -v and node --version?

There is no functional difference. According to the source code in src/node_options.cc, -v is declared as an alias for --version. Both trigger the same internal handler that prints NODE_VERSION_STRING and exits immediately.

How do I check the Node.js version in a shell script?

Capture the output of node -v in a variable or use it directly in a conditional statement. Because the command exits cleanly with code 0, it works reliably in bash, zsh, and POSIX-compliant shells:

INSTALLED=$(node -v)
[[ "$INSTALLED" == "v20.12.0" ]] && echo "Exact match"

Can I get the version without the 'v' prefix?

The standard CLI flags always include the v prefix (e.g., v20.12.0). To strip it in a shell script, use string manipulation:

node -v | cut -c2-

Why does node --version exit immediately?

The version check is implemented as an early-return path in the native C++ startup sequence. By design, src/node_options.cc causes the process to print the version stored in src/node_version.h and call exit(0) before the V8 JavaScript engine or libuv event loop initialize, ensuring minimal overhead.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s https://instagit.com/install.md

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client