Skip to the solution
EasyPhone Screen5 min read

What is the role of the path module and why is it preferred over manual string concatenation?

873 views

The problem

Question presented to candidate: "You see code building a file path with string concatenation, like folder + '/' + filename. What can actually go wrong with that, and what does Node's path module do differently?"

What a strong answer should cover:

  • Node's built-in path module builds and manipulates file-system paths correctly for the current operating system β€” path.join, path.resolve, path.basename, path.extname, path.dirname, and others.
  • πŸ“Œ The concrete bug manual concatenation introduces: joining path segments with a hardcoded "/" (or worse, no separator handling at all) produces double separators, missing separators, or wrong separators entirely depending on whether the input already ended in a slash and which OS is running β€” path.join normalizes all of that automatically.
  • path.join also correctly resolves relative segments like ".." and "." within the joined result, which naive concatenation does not do at all β€” it just concatenates literal strings.
  • Windows uses \ as its separator; POSIX systems (Linux, macOS) use /. Code that hardcodes either separator breaks on the other platform; path.join/path.resolve use path.sep, the correct separator for the platform actually running, automatically.
  • path.win32 and path.posix are explicitly available for code that needs to build a path for a specific platform regardless of which OS it currently runs on (e.g. generating a path string to embed in a config file meant for a different target platform) β€” distinct from the default path export, which always reflects the current platform.
  • A precise answer distinguishes path.join (concatenates segments, normalizes the result, does not resolve to an absolute path unless an input already was) from path.resolve (always produces an absolute path, resolving against process.cwd() if needed) β€” a commonly confused pair.

Clarifying questions expected:

  • "Does this code need to run correctly on both Windows and POSIX, or only one target platform?" β€” decides how much of the separator discussion actually matters.
  • "Is an absolute path needed, or just a correctly joined relative one?" β€” decides between path.join and path.resolve.

Code / implementation expected: Yes β€” showing path.join correctly normalizing a case where manual concatenation visibly breaks is the concrete, convincing part of the answer.

  • path
  • file-system
  • cross-platform

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 9 sections

Read the code

JavaScriptpath.join correctly normalizing separators and resolving .. where manual concatenation breaks, plus win32 vs posix
const path = require("path");

console.log(path.join("a/", "/b", "c"));   // a\b\c  (normalized, no double separator)
console.log("a/" + "/b" + "c");             // a//bc  (broken, literal double separator)

console.log(path.join("/foo/bar", "../baz")); // \foo\baz  (.. actually resolved)

console.log(path.win32.join("a", "b", "c")); // a\b\c
console.log(path.posix.join("a", "b", "c")); // a/b/c

console.log(path.resolve("a", "b"));          // an ABSOLUTE path, resolved against process.cwd()
console.log(path.join("a", "b"));             // stays relative: "a\b"

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 9 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

Explain how modules work in Node.js.

Continue