CodingNic

Introduction to Backend Development

Your First Node.js Program

Introduction to Backend Development 10 min read

Your First Node.js Program

Objectives

By the end of this lesson, you should be able to:

  • Write and run a Node.js file from the command line
  • Read command-line arguments passed to a Node.js program
  • Explain the difference between running JavaScript in Node.js versus a browser

⚠️ A note on verification: every command and output in this lesson was actually run with Node.js.

Writing and Running a File

Create a file named hello.js:

javascript
console.log("Hello, backend world!");

Run it from the terminal:

bash
node hello.js
text
Hello, backend world!

That’s the entire cycle: write JavaScript in a .js file, run it with node filename.js, see the output in the terminal. No browser, no HTML page, no <script> tag, Node.js runs JavaScript directly as a standalone program.

Node.js vs Browser JavaScript

JavaScript in a browser has access to things like the DOM (document, window) since it’s running inside a web page. Node.js has no browser around it at all, there’s no document or window, but it has things a browser script doesn’t: direct access to the file system, the ability to start a server and listen for network connections, and more, all covered in the next two modules.

The core JavaScript language itself, variables, functions, loops, objects, works identically in both environments, what differs is the surrounding environment and the extra capabilities each one provides.

Reading Command-Line Arguments

A Node.js program can read whatever extra arguments were passed on the command line, through process.argv:

javascript
console.log(process.argv);
bash
node args.js sam 27
text
[
  '/usr/bin/node',
  '/tmp/node101/args.js',
  'sam',
  '27'
]

process.argv is an array, the first two entries are always the path to Node itself and the path to the script being run, anything after that is the extra arguments actually passed in, 'sam' and '27' here. process, one of Node’s global objects, is covered properly in the next module.

A Small Program

javascript
function add(a, b) {
  return a + b;
}

console.log("2 + 3 =", add(2, 3));
console.log("10 + 32 =", add(10, 32));
bash
node sum.js
text
2 + 3 = 5
10 + 32 = 42

Nothing here is Node-specific yet, this is plain JavaScript, running as a standalone program instead of inside a web page. The next module reviews the modern JavaScript syntax used constantly from here forward, and the module after that starts using Node’s own specific capabilities.

Try It

  1. Write a file that prints your name and today’s date using console.log.
  2. Write a file that reads two command-line arguments using process.argv and prints their sum (remember, process.argv entries are strings, they’ll need converting to numbers first).
  3. Explain, in your own words, one thing Node.js can do that browser JavaScript can’t, and one thing browser JavaScript can do that Node.js can’t.

Recap

  • A Node.js program is a .js file run directly with node filename.js, no browser or HTML involved.
  • The core JavaScript language is identical in Node.js and the browser, the surrounding environment and available capabilities differ.
  • process.argv gives a running Node.js program access to whatever arguments were passed on the command line.

Next lesson: this module’s exercises, tying together everything covered so far.