Write and Run Hello World Without an Editor

Introduction

This chapter shows you how to write and run a JavaScript hello_world program without any code editor. You will create a file, execute it with Node.js, and fix common beginner mistakes from the terminal. This workflow matters on servers, in containers, and whenever GUI tools are unavailable.

Prerequisites

  • Node.js LTS installed and verified (node --version, npm --version)
  • Completed at least one install chapter: Windows, macOS, or Linux
  • Basic command-line knowledge (cd, listing files, creating files)

Why Learn Terminal-Only Hello World

Running scripts from the terminal teaches how Node.js actually starts your code.

Core benefits:

  • Confirm Node.js works outside browsers and IDEs
  • Build confidence with file paths and working directories
  • Practice steps that CI pipelines and deployment scripts reuse

Tip

Best Practice

Run commands from the folder that contains your .js file, or pass the full path to node so Node can find the script.

Step 1: Verify Node.js in Terminal

bash
# Check Node.js version
node --version

Expected: a version string such as v22.x.x, not a “command not found” error.

Step 2: Create hello.js Without an Editor

macOS / Linux

bash
# Create hello.js with a single console.log line
printf 'console.log("Hello, World!");\n' > hello.js

Windows PowerShell

powershell
# Create hello.js with a single console.log line
Set-Content -Path .\hello.js -Value 'console.log("Hello, World!");'

Windows Command Prompt

bat
:: Create hello.js (quotes matter on Windows CMD)
echo console.log("Hello, World!");> hello.js

Step 3: Run the Script with Node.js

bash
# Execute hello.js with the Node.js runtime
node hello.js

Expected output:

text
Hello, World!

Step 4: Understand What Happened

  • hello.js: your source file (plain text, UTF-8)
  • node: the runtime that reads the file and executes JavaScript line by line
  • console.log: prints text to the terminal (similar to browser DevTools, different environment)

Unlike Java, you do not compile to a separate bytecode file for this simple script—Node runs the source directly (with internal optimization).

Common Issues

SymptomLikely causeFix
Cannot find module for hello.jsWrong working directorycd to the folder containing hello.js, or pass the full path
Syntax error near unexpected tokenSmart quotes or broken echo on WindowsRecreate file with the PowerShell Set-Content example
No outputEmpty file or wrong filenamels / dir and open the file content to confirm
bash
# List files in the current directory (macOS/Linux)
ls -la
 
# Print file contents to verify
cat hello.js

Optional: Pass a Greeting from the Command Line

javascript
// hello.js — read the first CLI argument
const name = process.argv[2] ?? "World";
console.log(`Hello, ${name}!`);
bash
# Run with an argument after the script name
node hello.js Ada

Expected output: Hello, Ada!

FAQ

Is console.log only for beginners?

No. Developers use console.log (and richer logging libraries) throughout real projects for debugging and operational visibility.

Should the file be hello.js or hello_world.js?

Either name works if you pass the correct path to node. Use consistent, descriptive names in projects (greet.js, main.js, etc.).

Can I run JavaScript without saving a file?

Yes—try node with no file to enter a REPL (read-eval-print loop). Files are clearer for anything you want to keep or share.

Why not use the browser for this chapter?

Browsers run JavaScript too, but this track standardizes on Node.js for scripts and tooling. The next editor chapters still apply to browser work later.

What is process.argv?

An array of command-line arguments. Element 0 is node, 1 is the script path, and 2+ are your custom arguments.