path and os

path.join

Never glue file paths with plus and separators by hand. The join helper picks the right separator and removes duplicate parts.

import { join } from "node:path"; const file: string = join("notes", "app.txt"); console.log(file);

notes/app.txt

The result adapts to the host system automatically. The same call works on every desktop OS without manual tweaks.

__dirname note

Scripts often need files next to their own source, not next to the launch folder. In CommonJS, the dirname value points at the script folder for exactly this case.

ESM uses import.meta.url for the same job. Stay with CommonJS here and treat the ESM name only as a landmark for later reading.

import { join } from "node:path"; const nearby: string = join(__dirname, "notes.txt"); console.log(nearby);

OS facts

The os module reports platform and home facts without shell calls. Use them for diagnostics and for picking sensible defaults.

import { platform, homedir } from "node:os"; console.log(platform()); console.log(homedir());

The first line is the platform name, and the second is the home folder of the user. The home path is absolute and differs on every machine, so it is not repeated as a fixed output here.

Config paths

Per-user settings belong under the home folder, joined with path logic. That placement keeps projects portable and avoids hard-coded locations.

import { homedir } from "node:os"; import { join } from "node:path"; const configFile: string = join(homedir(), "app-config.json"); console.log(configFile);

Build the full path from parts at runtime. Logging it once during development confirms the file lands where users expect.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why build file paths with path.join instead of plus signs?

    Answer

    It uses the separator of the host and removes duplicate parts

    The helper owns the separator and the normalisation, so the same call works on every OS.

  2. What does __dirname name in a CommonJS script?

    Answer

    The folder of the script

    The value points at the script folder, not the working directory of the shell.

  3. What does os.platform() report?

    Answer

    The name of the operating system

    The call returns a short platform name such as linux, darwin, or win32.

  4. Where does a per-user config file belong?

    Answer

    Under the home folder, joined with path logic

    The home placement keeps the project portable and avoids hard-coded absolute paths.

  5. What does this program print? import { join } from "node:path"; console.log(join("notes", "app.txt"));

    Answer

    notes/app.txt

    The join puts one separator between the folder and the file name.

  6. What does this program print? import { join } from "node:path"; console.log(join("a", "..", "b"));

    Answer

    b

    The join normalises the parent step away, leaving b.

  7. What does this program print? console.log("app-config.json".split(".").length);

    Answer

    2

    The string splits at the one dot, which makes two parts.

  8. What does this program print? console.log(["a", "b"].join("/"));

    Answer

    a/b

    The join puts the slash between the two parts.

  9. What does the compiler say about this program? import { join } from "node:path"; const file: string = join(1, "a.txt"); console.log(file);

    Answer

    error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

    join takes string parts, and the number 1 does not fit.

  10. What does the compiler say about this program? const y: number = __dirname; console.log(y);

    Answer

    error TS2322: Type 'string' is not assignable to type 'number'.

    __dirname is a string path, and a number variable cannot hold it.

Article author: Arthur Isaev