Interview Q&A / Node.js

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the difference between path.join and path.resolve?

SHORT ANSWER

join just glues segments together with the correct separator. resolve builds an absolute path, processing segments right to left until it has one, and falls back to the current working directory.

Explain it simply

path.join is like writing an address by writing the parts one after the other: "Lane 4, Block B, Sector 5". The result may stay relative. path.resolve behaves like asking, "Starting from where I am standing, what is the full address?" It always gives a full, absolute path.

Example

Assume the current working directory is /home/app.

js
const path = require("path");

console.log(path.join("src", "index.js"));
// prints: src/index.js

console.log(path.resolve("src", "index.js"));
// prints: /home/app/src/index.js

console.log(path.join("/data", "/reports", "a.txt"));
// prints: /data/reports/a.txt

console.log(path.resolve("/data", "/reports", "a.txt"));
// prints: /reports/a.txt

The last pair is the key difference. resolve works from the right: when it meets a segment that starts with /, it treats that as the root and ignores everything before it. join does not care; it just concatenates and cleans.

Use join to build a path from known pieces. Use resolve when you need an absolute path, for instance to check that a file stays inside a base folder.

What to say in the interview

"join concatenates and normalises; resolve produces an absolute path, and a segment starting with a slash resets it. I use resolve when I need a full path or want to compare against a base directory."

Common mistakes

  • Expecting resolve to keep earlier segments after an absolute one.
  • Relying on the working directory when you should use __dirname.

#nodejs#path

Related questions

Practise more Node.js questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Node.js quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.