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:
console.log("Hello, backend world!");
Run it from the terminal:
node hello.js
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:
console.log(process.argv);
node args.js sam 27
[
'/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
function add(a, b) {
return a + b;
}
console.log("2 + 3 =", add(2, 3));
console.log("10 + 32 =", add(10, 32));
node sum.js
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
- Write a file that prints your name and today’s date using
console.log. - Write a file that reads two command-line arguments using
process.argvand prints their sum (remember,process.argventries are strings, they’ll need converting to numbers first). - 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
.jsfile run directly withnode 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.argvgives 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.