Skip to content

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.

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:os module
    • package.json Project configuration with "type": "module"
  1. Create the folder and initialize the project

    Open the terminal in Visual Studio Code and run:

    Finestra del terminale
    node -v
    mkdir lab-node-intro
    cd lab-node-intro
    npm init -y

    Note:

    • Use mkdir and cd to create the project subfolder.
    • If you have already created the subfolder and opened it in VS Code, do not run mkdir and cd.
  2. Enable ES modules in package.json

    • Make sure "type": "module" appears only once in package.json. This setting declares that the project uses ES modules, which enables the modern import and export syntax that you’ll use in app.js instead of the older require() (CommonJS).
    • Make sure package.json contains “main”: “app.js”
  3. Create and run app.js Create the app.js file 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
  4. You will see text like this in the terminal:

    Ciao ilTuoNome! Node.js è attivo su win32 (x64) alle 17:49:10.

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().username

    Correct: os.userInfo() returns information about the current user, and .username extracts 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, and os.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 import syntax instead of the older require() (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