CodingNeed.

1 · Get comfortable · 18 MIN

Run a JavaScript file outside the browser

Node.js runs JavaScript with server and operating-system APIs.

The browser provides window and document for working with a page. Node.js provides process and modules for work such as files and network servers; it does not create a browser DOM. A .mjs file uses JavaScript modules. Run it from a terminal and read its printed output there. process.argv contains the executable, script path and additional arguments; index 2 is the first value you type after the filename. Those arguments are strings until you convert them.

Check node --version, then save the .mjs file. Run node lesson.mjs Ada in the folder containing it. Compare argv[2] with the first extra argument. Read argv[3] for the optional topic.

Before you start

Install Node.js 22.12+ (or a supported newer LTS). Create a folder named codingneed-node. Open a terminal in that folder. Save ONE lesson example as lesson.mjs and run node lesson.mjs. No npm dependencies are required. HTTP lessons keep running until you press Ctrl+C; stop the previous server before starting another.

File for this example: lesson.mjs

New words, explained

runtime
The program that executes JavaScript and supplies APIs.
terminal
A text interface for starting programs.
argument
A value supplied when invoking a program or function.

Follow the example step by step

  1. Check node --version, then save the .mjs file.
  2. Run node lesson.mjs Ada in the folder containing it.
  3. Compare argv[2] with the first extra argument.
  4. Read argv[3] for the optional topic.

You are ready to move on when: The script runs without arguments. Two arguments appear in the greeting. Explain why document.querySelector is unavailable here.

Read the example

// Save this complete file as lesson.mjs.
const name = process.argv[2] ?? 'learner';
console.log('Hello, ' + name);
console.log('Arguments are ' + typeof name);
console.log('This program runs in Node.js, not in a browser page.');
Check the expected output
node lesson.mjs Ada prints Hello, Ada, then Arguments are string, then the runtime explanation. Without Ada the greeting uses learner.

Your challenge

Accept a second argument for a learning topic and include it in the greeting. Supply a default topic when omitted.

Solution cost: Discuss the operations in this small example; rendering and I/O costs depend on the host. time · Proportional to the example’s retained data. space

Common trap

Running code in a terminal is different from typing code into the operating-system shell.

Study the project implementation
// Save this complete file as lesson.mjs.
const name = process.argv[2] ?? 'learner';
console.log('Hello, ' + name);
console.log('Arguments are ' + typeof name);
console.log('This program runs in Node.js, not in a browser page.');

Further reading: Official documentation

Next lesson: Understand imports and explicit exports

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.