First script with Node.js
Before building the real-time chat with WebSocket and ws, it is useful to check that Node.js works correctly on your computer and see how JavaScript runs on the server rather than inside a browser tab.
Unlike scripts running in a browser—which interact with the DOM and window—a Node.js script runs directly in the operating system’s runtime and can access built-in modules such as node:os.
Introduction
Section titled “Introduction”Project structure
Section titled “Project structure”Create a dedicated folder for this preparatory exercise to keep it separate from the WebSocket project:
Directorylab-node-intro/
- app.js A small script that uses the built-in
node:osmodule - package.json Project configuration with
"type": "module"
- app.js A small script that uses the built-in
Step-by-step instructions
Section titled “Step-by-step instructions”-
Create the folder and initialize the project
Open the terminal in Visual Studio Code and run:
Finestra del terminale node -vmkdir lab-node-introcd lab-node-intronpm init -yNote:
- Use
mkdirandcdto create the project subfolder. - If you have already created the subfolder and opened it in VS Code, do not run
mkdirandcd.
- Use
-
Enable ES modules in
package.json- Make sure
"type": "module"appears only once inpackage.json. This setting declares that the project uses ES modules, which enables the modernimportandexportsyntax that you’ll use inapp.jsinstead of the olderrequire()(CommonJS). - Make sure
package.jsoncontains “main”: “app.js”
- Make sure
-
Create and run
app.jsCreate theapp.jsfile with the code below:app.js import os from 'node:os';const username = os.userInfo().username;const platform = `${os.platform()} (${os.arch()})`;const currentTime = new Date().toLocaleTimeString('it-IT');console.log(`Ciao ${username}! Node.js è attivo su ${platform} alle ${currentTime}.`);Then run it from the terminal:
Finestra del terminale node app.js -
You will see text like this in the terminal:
Ciao ilTuoNome! Node.js è attivo su win32 (x64) alle 17:49:10.
Knowledge check
Section titled “Knowledge check”Your answers to the quizzes on this page are saved in your browser's local storage and remain saved between visits.
Which property or method from the node:os module did we use to read the current user’s name?
-
os.platform() -
os.userInfo().usernameCorrect:
os.userInfo()returns information about the current user, and.usernameextracts the name. -
os.arch()
In the message printed in the terminal, what do os.platform() and os.arch() represent?
- The Node.js version and the project folder name.
- The operating system (e.g.
win32,darwin,linux) and processor architecture (e.g.x64,arm64).Correct:
os.platform()identifies the operating system platform, andos.arch()identifies the hardware architecture on which the runtime runs. - The local IP address and network port.
Why do we set "type": "module" in package.json for the app.js script?
- To allow the modern
importsyntax instead of the olderrequire()(CommonJS). - To automatically run the file in a browser using
Live Server. - To disable
console.log()during execution.
Quiz progress
0 / 0 questions answered (0%)
0 correct answers
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0