Paths with node:path

A path looks like a string and is not one: it has a separator that depends on the system, a parent marker that has to be resolved, and a difference between a name and the file it may or may not point at. node:path is the module that knows all three, and it is pure: it reads no file and touches no disk, so the four programs of this lesson run without creating anything.

The lesson joins a path and compares the result with the plus sign, resolves a sequence against a base folder, takes a file name apart and ends with the cases where an extension is not where a reader expects it. Every absolute path printed here starts with /srv or comes from a fixed base, so the output is the same on every machine that runs Linux.

Join against concatenation

The plus sign does exactly what it says: it puts two strings next to each other. That is not a path operation, so a missing separator gives dataa.txt, a doubled one gives data//a.txt, and a .. stays in the middle of the string instead of cancelling the folder before it. join does those three things, and it uses the separator of the system it runs on.

The program builds one path seven ways, four by hand and three with join, and prints them in order.

The file concat.mjs:

import { join } from 'node:path'; const part = 'notes'; console.log('by hand:', 'data/' + part + '/a.txt'); console.log('with a missing separator:', 'data' + 'a.txt'); console.log('with a doubled separator:', 'data/' + '/' + 'a.txt'); console.log('with join:', join('data', part, 'a.txt')); console.log('join drops an empty segment:', join('data', '', 'a.txt')); console.log('by hand the parent stays:', 'data/../a.txt'); console.log('join resolves it:', join('data', '..', 'a.txt'));

node concat.mjs

by hand: data/notes/a.txt
with a missing separator: dataa.txt
with a doubled separator: data//a.txt
with join: data/notes/a.txt
join drops an empty segment: data/a.txt
by hand the parent stays: data/../a.txt
join resolves it: a.txt

Two of the hand-written paths would still work, because the operating system tolerates a doubled separator and resolves the dots itself; the empty segment is the one that gives a wrong file name. The three join lines are correct by construction, and the last one shows that a parent step is resolved rather than carried along.

resolve

resolve walks the parts from right to left until it has an absolute path, which makes it the function for turning a config value or a command-line argument into a path the program can open. An absolute part restarts the walk, and a relative result is resolved against the working directory, which is why the program always passes a base of its own.

The base is /srv/app in all four lines, so the output does not depend on where the program was started. The second line goes one folder up, the third meets an absolute part, and the fourth carries a parent step that disappears.

The file resolve.mjs:

import { resolve } from 'node:path'; const base = '/srv/app'; console.log('a child path:', resolve(base, 'data', 'notes.txt')); console.log('a parent step:', resolve(base, '..', 'logs')); console.log('an absolute part wins:', resolve(base, '/etc/hosts')); console.log('the dots are resolved:', resolve(base, './data/../notes.txt'));

node resolve.mjs

a child path: /srv/app/data/notes.txt
a parent step: /srv/logs
an absolute part wins: /etc/hosts
the dots are resolved: /srv/app/notes.txt

The absolute part in the third call is the one surprise: /etc/hosts does not sit under the base, and resolve says so. That behaviour is what a command-line option wants, since a user who gives an absolute path means it, and it is also why a program must not pass a user-controlled absolute path into a folder it owns without checking it.

basename, dirname and extname

The three functions answer the three questions a program asks about a path: what is the last component, what is the folder above it, and what is the extension. parse answers all of them at once and format builds a path back from such an object.

The program takes apart a path built with join and prints each answer, including the name without the extension and the parsed object itself.

The file names.mjs:

import { basename, dirname, extname, join, parse } from 'node:path'; const file = join('reports', '2026', 'summary.csv'); console.log('the path:', file); console.log('basename:', basename(file)); console.log('dirname:', dirname(file)); console.log('extname:', extname(file)); console.log('without the extension:', basename(file, '.csv')); console.log('parsed:', JSON.stringify(parse(file)));

node names.mjs

the path: reports/2026/summary.csv
basename: summary.csv
dirname: reports/2026
extname: .csv
without the extension: summary
parsed: {"root":"","dir":"reports/2026","base":"summary.csv","ext":".csv","name":"summary"}

The parsed object keeps the extension with its dot, so joining name and ext gives the file name back. dirname of a bare name is a dot, not an empty string, and that dot is what makes a relative path still work when the program changes the folder it writes into.

Extension traps

An extension is the part after the last dot of the last component, which gives three cases that catch programs: a dot file such as .gitignore has no extension at all, an archive named .tar.gz has only .gz, and a dot in a folder name has nothing to do with the file inside it. A tool that decides by extension should also accept the files it does not recognise.

The program prints the extension of four names, then a path from one folder to another and two answers from isAbsolute.

The file traps.mjs:

import { extname, isAbsolute, relative } from 'node:path'; console.log('a dot file:', JSON.stringify(extname('.gitignore'))); console.log('two extensions:', JSON.stringify(extname('archive.tar.gz'))); console.log('no extension:', JSON.stringify(extname('Makefile'))); console.log('a dot in a folder:', JSON.stringify(extname('v1.2/README'))); console.log('from one folder to another:', relative('/srv/app/data', '/srv/app/logs/today.log')); console.log('absolute or not:', isAbsolute('data/notes.txt'), isAbsolute('/srv/app'));

node traps.mjs

a dot file: ""
two extensions: ".gz"
no extension: ""
a dot in a folder: ""
from one folder to another: ../logs/today.log
absolute or not: false true

relative is the inverse of resolve and is what a report needs when it prints a path next to a base folder. Together with the other three functions it means a program never has to count separators by hand, and the lesson on directories uses all of them to build and walk a tree.

Exercises

  1. Build the path of a file two folders up from /srv/app/data with join and again with resolve. Do the two agree?
  2. Print parse of a file with no extension. Which fields are empty strings?
  3. Ask extname for the extension of a name that ends with a dot. What does it answer, and what would your own code answer?
  4. Change the extension of a file to .json with parse and format. Which fields do you have to set?
  5. What does join do with an absolute second part, and what does resolve do with the same part? Run both and explain the difference.

Questions

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

  1. What does join do with a parent step?

    Answer

    It resolves it, so the folder before it disappears

    join normalises the path, and resolving the dots is part of that.

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

    Answer

    data/a.txt

    An empty segment adds no separator of its own, so the result is a clean path.

  3. What does extname return for archive.tar.gz?

    Answer

    .gz

    An extension starts at the last dot of the last component.

  4. What does this program print? import { resolve } from "node:path"; console.log(resolve("/srv/app", "/etc/hosts"));

    Answer

    /etc/hosts

    An absolute part restarts the resolution, so the base is dropped.

  5. Which separator does join use?

    Answer

    The one of the system the program runs on

    The module follows the platform, and path.posix and path.win32 exist for the other two cases.

  6. What does this program print? import { basename } from "node:path"; console.log(basename("reports/2026/summary.csv", ".csv"));

    Answer

    summary

    The second argument is removed when the name ends with it.

  7. Which function answers with the folder above a file?

    Answer

    dirname

    dirname returns the part before the last separator.

  8. What does extname return for .gitignore?

    Answer

    An empty string

    A name that is only a dot and letters has no extension.

  9. Does node:path touch the disk?

    Answer

    No, it only works with strings

    The functions are pure string operations over a path, and that is why the lesson needs no files.

  10. What does this program print? import { join } from "node:path"; console.log(join("reports", "2026", "..", "summary.csv"));

    Answer

    reports/summary.csv

    The parent step cancels 2026 and leaves the file directly in reports.

The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev