How to Install Node.js and What is Node.js?
What you are installing
Node.js runs JavaScript outside the browser. This exercise uses it to calculate study time from a small array, without a web server or third-party dependencies. You should understand variables, objects and basic arithmetic before starting.
Choose a supported version
Use the official download reference below. Select a supported LTS release compatible with your operating system and any existing project's requirements. Follow the instructions for your platform and installation method; avoid mixing several installers without checking which executable your terminal uses.
Verify the installation
Open a new terminal after installation:
node --version
npm --versionBoth commands should print a version. If Node is not found, check the installation and PATH rather than running your application as administrator. On PowerShell, Get-Command node shows the selected executable. On macOS or Linux, use command -v node.
Make a study-time calculator
Create a file named study-total.mjs in a folder you can find again:
const sessions = [
{ topic: 'Algebra', minutes: 25 },
{ topic: 'Physics', minutes: 30 },
{ topic: 'Review', minutes: 10 }
];
const total = sessions.reduce((sum, session) => sum + session.minutes, 0);
const hours = Math.floor(total / 60);
const minutes = total % 60;
console.log('Total: ' + total + ' minutes (' + hours + 'h ' + minutes + 'm)');In that folder, run:
node study-total.mjsExpected output:
Total: 65 minutes (1h 5m)The reducer starts at zero and adds each session. Dividing by 60 and rounding down gives whole hours; the remainder operator gives the minutes left over. All minutes in this exercise are numbers, not quoted strings.
Check the calculation yourself
- Change Review from 10 to 20: expect 75 minutes, or 1h 15m.
- Set sessions to an empty array: expect zero hours and minutes.
- Add a session with zero minutes: the total should not change.
If you later read user input, validate it before summing: reject negative, non-finite and non-numeric values rather than silently producing a misleading total.
Common errors
A module-not-found message can mean you are in the wrong folder or the filename is incorrect. Check that the editor did not save study-total.mjs.txt. A syntax error usually points to a missing quote, comma or bracket; compare the indicated line with the example. If different terminals report different versions, inspect their selected executable paths before reinstalling.
Where npm fits
This example needs no npm install step because it uses only JavaScript features. Larger projects describe dependencies in package.json. Review dependencies before installing them, keep supported versions updated, and test changes rather than applying automatic fixes blindly. As a next exercise, print a separate line for each topic and verify that those lines add up to the displayed total.
Found an error? Send a correction. Read our editorial policy.
Reference →