๐Ÿ Node.js Introduction & Getting Started
Estimated reading: 3 minutes 278 views

Node.js โ€“ Command Line Options โ€“ Master Node.js CLI Flags for Efficient Development


Introduction โ€“ Why Learn Node.js Command Line Options?

Node.js provides a rich set of command line options (CLI flags) that give you more control over how Node executes JavaScript code. These flags allow developers to enable debugging, run code snippets, measure performance, set environment variables, and customize the Node.js runtime behavior.

Understanding these options helps you become a more efficient and powerful Node.js developer, especially when working with tools, scripts, or in DevOps environments.

In this guide, youโ€™ll learn:

  • How to use Node.js command line options
  • The most commonly used CLI flags
  • Practical examples of command usage
  • Debugging, scripting, and environment control via CLI

How to Use Node.js Command Line Options

To view all available CLI options, use:

node --help

Output:
Youโ€™ll get a long list of flags like --inspect, --trace-warnings, --eval, --version, etc.

To use a flag, simply type:

node [option] [script.js]

Or run inline code like:

node -e "console.log('Hello from Node CLI')"

Commonly Used Node.js CLI Flags

Hereโ€™s a table of the most helpful Node.js CLI options with examples:

Option Description Example
-v or --versionDisplays Node.js versionnode -v
-eExecutes inline JavaScriptnode -e "console.log(5 + 5)"
-pPrints result of the expressionnode -p "3 * 7"
--inspectEnables Chrome DevTools debuggernode --inspect app.js
--inspect-brkBreaks before first line for debuggingnode --inspect-brk app.js
--trace-warningsDisplays warnings with stack tracesnode --trace-warnings app.js
--experimental-modulesEnables experimental ES module supportnode --experimental-modules app.mjs
--requirePreloads modules before executionnode --require dotenv/config app.js
--trace-deprecationLogs use of deprecated featuresnode --trace-deprecation app.js

Examples โ€“ Running Node.js Commands with Flags

Print Output Directly

node -p "10 * 10"

Output:

100

Debug with Chrome DevTools

node --inspect-brk server.js

Then open chrome://inspect in your browser to start debugging.

Execute Inline JavaScript

node -e "console.log('Inline code run successfully!')"

Output:

Inline code run successfully!

Advanced Node.js Flags

Option Use Case
--max-old-space-size=SIZESet memory limit in MB (useful for heavy apps)
--trace-gcLogs garbage collection activity
--abort-on-uncaught-exceptionCrash app on uncaught exceptions (for diagnostics)
--es-module-specifier-resolution=nodeCustomize ES module path resolution behavior

Node.js Environment Variable Flags

Node.js supports setting environment variables inline:

NODE_ENV=production node app.js   # UNIX

On Windows (CMD):

set NODE_ENV=production && node app.js

Or PowerShell:

$env:NODE_ENV="production"; node app.js

Best Practices Using Node CLI Options

Practice Tip
Use --inspect for debuggingCombine with DevTools or VS Code breakpoints
Use -e/-p for quick testsAvoid creating files for one-liners or quick math
Log deprecations & warningsUse --trace-deprecation and --trace-warnings
Profile memory & performanceUse --trace-gc and --max-old-space-size
Automate with CLI scriptsUse in npm run scripts and CI/CD pipelines

Summary โ€“ Recap & Next Steps

Node.js CLI options are powerful tools that can boost your development, debugging, and deployment efficiency. From running inline code to inspecting memory leaks, mastering these flags will help you unlock Node.jsโ€™s full capabilities.

Key Takeaways:

  • CLI options let you control how Node.js executes code
  • Use flags like --inspect, -e, -p, and --trace-warnings regularly
  • Useful in automation scripts, debugging, and quick experimentation

Real-world relevance:
Used in production setups, testing workflows, deployment scripts, and debugging environments.


FAQs โ€“ Node.js Command Line Options


How do I see all Node.js CLI options?
Run:

node --help

Whatโ€™s the difference between -e and -p?
-e executes JavaScript; -p evaluates and prints the result automatically.


How can I debug Node.js apps with Chrome?
Use:

node --inspect-brk app.js

Then open chrome://inspect.


Can I preload modules before app execution?
Yes. Use --require flag:

node --require dotenv/config app.js

Share Now :
Share

๐Ÿงญ Node.js โ€“ Command Line Options

Or Copy Link

CONTENTS
Scroll to Top