Skip to content
Laviyx Learn

Try

Node.js and Express course menu

How to Install Node.js and Run JavaScript Files

Install Node.js, check the version, run JavaScript files with node, use the REPL, restart automatically with --watch, read command-line output, and choose an editor and LTS version.

Beginner5 min readUpdated
On this page

Install Node.js#

Download the LTS (long-term support) version from nodejs.org. LTS releases get fixes for about three years and are what companies use in production. The installer includes npm, the package manager.

Check the installation in a terminal:

Terminal
node --version
npm --version
Output
v24.15.0
11.12.1

(These are the versions this course was run with. Any current LTS version works for the examples.)

Developers who switch between projects often install Node with a version manager such as nvm (macOS/Linux), nvm-windows or fnm, so each project can use the Node version it needs.

Run a file#

Save this as hello.js:

hello.js (JavaScript)
const name = "Laviyx";
const hour = 9;
const greeting = hour < 12 ? "Good morning" : "Good afternoon";
console.log(`${greeting}, ${name}!`);

and run it from the same folder:

Terminal
node hello.js
Output
Good morning, Laviyx!

node runs the file from top to bottom and exits when there is nothing left to do (no open servers, timers or pending operations).

The REPL#

Running node with no file opens the REPL (read-eval-print loop): type JavaScript and see the result immediately. It is handy for trying things out:

Text
> 2 ** 10
1024
> "node".toUpperCase()
'NODE'
> .exit

Press Ctrl+C twice or type .exit to leave.

Restart on changes with --watch#

While developing a server, you want it to restart whenever you save a file:

Terminal
node --watch server.js

Node then watches the file (and what it imports) and restarts it on every change. Older projects use the nodemon package for the same job.

Errors and stack traces#

When something goes wrong, Node prints the error and a stack trace showing where it happened:

broken.js (JavaScript)
const prices = [120, 80];
console.log(prices.total.toFixed(2));
Terminal
node broken.js
Output
file:///C:/Projects/laviyx-course-demo/my-app/broken.js:2
console.log(prices.total.toFixed(2));
                         ^

TypeError: Cannot read properties of undefined (reading 'toFixed')
    at file:///C:/Projects/laviyx-course-demo/my-app/broken.js:2:26
    at ModuleJob.run (node:internal/modules/esm/module_job:437:25)
    at async node:internal/modules/esm/loader:639:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

Node.js v24.15.0

Read it from the top: the file and line (broken.js:2), the line of code with a ^ under the problem, and the error itself. prices.total is undefined, so calling .toFixed on it fails. (Folder paths in the trace depend on where your file is.)

Editor#

VS Code is the most common editor for Node.js: it has a built-in terminal, debugger and JavaScript support. Run node --inspect file.js (or use VS Code's "JavaScript Debug Terminal") to set breakpoints and step through code.

Common mistakes#

Practice#

Practice exercise

Write bill.js that calculates a restaurant bill: items costing 250, 180 and 90, 5% GST, and a tip of 10% on the amount before tax. Print the subtotal, GST, tip and total, each with 2 decimals.

Show hint

Add up the items with reduce, then compute each part and use toFixed(2) when printing.

Show solution
bill.js (JavaScript)
const items = [250, 180, 90];
const subtotal = items.reduce((sum, price) => sum + price, 0);
const gst = subtotal * 0.05;
const tip = subtotal * 0.1;
console.log("Subtotal:", subtotal.toFixed(2));
console.log("GST 5%:", gst.toFixed(2));
console.log("Tip 10%:", tip.toFixed(2));
console.log("Total:", (subtotal + gst + tip).toFixed(2));
Terminal
node bill.js
Output
Subtotal: 520.00
GST 5%: 26.00
Tip 10%: 52.00
Total: 598.00

Key takeaway#

Next: Modules: import and export.

Finished this lesson?