🔥 0
0
Lesson 2 of 10 10 min +50 XP

Your First Node Script

Let's write real Node.js code. You'll create files, run them, and explore the interactive REPL.

Writing Your First Script

Create a file called hello.js:

// hello.js
console.log("Hello, Node.js!");
console.log("This is running on your computer, not in a browser.");

Run it:

node hello.js
Output:
Hello, Node.js!
This is running on your computer, not in a browser.

Congratulations - you just ran server-side JavaScript!

The Node.js REPL

REPL = Read-Eval-Print-Loop

It's an interactive shell for quick experiments. Start it:

node
You'll see:
Welcome to Node.js v20.10.0.
Type ".help" for more information.
>

Try some JavaScript:

> 2 + 2
4
> "hello".toUpperCase()
'HELLO'
> const numbers = [1, 2, 3]
undefined
> numbers.map(n => n * 2)
[ 2, 4, 6 ]
> .exit

Type .exit or press Ctrl+C twice to quit.

No window, No document

In browsers, you have window and document. In Node.js, they don't exist:

// browser.js - This would work in a browser
console.log(window.location.href);
document.getElementById('app');

// node.js - These will throw errors
// ReferenceError: window is not defined
// ReferenceError: document is not defined

Instead, Node.js has its own globals.

The process Object

process is Node's most important global. It gives you info about the running program:
// process-demo.js
console.log("Node version:", process.version);
console.log("Current directory:", process.cwd());
console.log("Platform:", process.platform);
Run it:
node process-demo.js
Output:
Node version: v20.10.0
Current directory: /Users/you/projects
Platform: darwin

Command-Line Arguments

process.argv contains arguments passed to your script:
// greet.js
const name = process.argv[2] || "World";
console.log(`Hello, ${name}!`);
Run it:
node greet.js
node greet.js Alice
node greet.js "Bob Smith"
Output:
Hello, World!
Hello, Alice!
Hello, Bob Smith!
How argv works:
// If you run: node greet.js Alice
process.argv[0]  // '/usr/local/bin/node' (node executable)
process.argv[1]  // '/path/to/greet.js' (script path)
process.argv[2]  // 'Alice' (first argument)

Environment Variables

Access environment variables with process.env:

// env-demo.js
console.log("User:", process.env.USER);
console.log("Home:", process.env.HOME);
console.log("Custom:", process.env.MY_VAR || "not set");
Run with a custom variable:
MY_VAR=hello node env-demo.js
Output:
User: yourname
Home: /Users/yourname
Custom: hello

This is how apps read configuration (API keys, database URLs, etc.).

Try It Yourself

Create calculator.js:

// calculator.js
const num1 = parseFloat(process.argv[2]);
const operator = process.argv[3];
const num2 = parseFloat(process.argv[4]);

let result;

switch (operator) {
  case '+': result = num1 + num2; break;
  case '-': result = num1 - num2; break;
  case 'x': result = num1 * num2; break;
  case '/': result = num1 / num2; break;
  default:
    console.log("Usage: node calculator.js <num1> <op> <num2>");
    console.log("Operators: + - x /");
    process.exit(1);
}

console.log(`${num1} ${operator} ${num2} = ${result}`);
Run it:
node calculator.js 10 + 5
node calculator.js 20 x 3
node calculator.js 100 / 4
Output:
10 + 5 = 15
20 x 3 = 60
100 / 4 = 25

You just built a command-line tool!

Crio.Do

Want a pre-configured Node.js environment?

Get dedicated cloud VMs with Node.js, databases, and dev tools pre-installed. Skip the setup, start building.

Start Free Trial

Key Takeaways

  • node filename.js runs a script
  • node (no args) starts the REPL
  • process.argv gives you command-line arguments
  • process.env gives you environment variables
  • No window/document - Node.js has different globals

Next, let's organize code with modules!

🧠 Quick Quiz

Test your understanding of this lesson.

1

How do you run a Node.js file called app.js?

2

What is the Node.js REPL?

3

Which object contains command-line arguments in Node.js?

What is Node.js?