Node.js - Command Line Options

Hello, future Node.js developers! I'm thrilled to guide you through the exciting world of Node.js command line options. As someone who's been teaching programming for years, I can assure you that mastering these options will make your coding journey much smoother. Let's dive in!

Node.js - Command Line Options

What are Command Line Options?

Before we start, let's understand what command line options are. Think of them as special instructions you give to Node.js when you're starting it up. They're like telling a car how to drive before you even turn the key!

Common Node.js Command Line Options

Let's explore some of the most useful command line options in Node.js. I'll provide examples for each, and we'll break them down together.

Display Version

One of the simplest yet most useful options is to check your Node.js version. Here's how you do it:

node --version

or its shorthand:

node -v

When you run this, you'll see something like:

v14.17.0

This tells you that you're running Node.js version 14.17.0. It's crucial to know your version, as different versions might have different features or behaviors.

Evaluate Script

Sometimes, you want to run a quick piece of JavaScript without creating a file. The -e or --eval option lets you do just that:

node -e "console.log('Hello, World!')"

This will output:

Hello, World!

It's like having a mini JavaScript playground right in your terminal! I often use this to test small snippets of code or demonstrate concepts to my students.

Show Help

When you're stuck or can't remember an option, the help command is your best friend:

node --help

This will display a list of all available command line options. It's like having a cheat sheet always at your fingertips!

Start REPL

REPL stands for Read-Eval-Print Loop. It's an interactive programming environment where you can type JavaScript code and see the results immediately. To start it, simply type:

node

You'll see a > prompt where you can start typing JavaScript:

> console.log('Hello from REPL!')
Hello from REPL!
undefined
> 2 + 2
4

The REPL is fantastic for learning and experimenting with JavaScript. It's like a sandbox where you can play without fear of breaking anything!

Load Module

Node.js allows you to preload modules before running your script. This is super useful for things like debugging or setting up environments. Here's how you use it:

node -r ./my-module.js my-app.js

In this example, my-module.js will be loaded before my-app.js starts running. It's like packing your backpack before going on a hike – you're making sure you have everything you need before you start!

Putting It All Together

Now that we've covered these options individually, let's see how we might use them in combination:

node -r ./debug-module.js --inspect my-app.js

This command does three things:

  1. It preloads a debug module
  2. It enables the Node.js debugger
  3. It runs your my-app.js file

It's like turning on all the special features in a video game before starting to play!

Command Line Options Table

Here's a handy table summarizing the options we've discussed:

Option Description Example
--version, -v Display Node.js version node -v
--eval, -e Evaluate JavaScript node -e "console.log('Hello')"
--help Show help node --help
(no option) Start REPL node
-r, --require Preload module node -r ./my-module.js app.js

Conclusion

Command line options in Node.js are powerful tools that can significantly enhance your development experience. They're like the secret buttons and cheat codes in a video game – once you know them, you can do so much more!

Remember, practice makes perfect. Don't be afraid to experiment with these options. Try combining them, see what happens, and most importantly, have fun!

As I always tell my students, coding is an adventure. These command line options are your map and compass. Use them wisely, and they'll guide you to exciting new territories in the world of Node.js development.

Happy coding, future Node.js masters!

Credits: Image by storyset