Full answer
Building paths with string concatenation breaks across platforms. Linux and macOS use /, Windows uses . The path module uses the right separator, and handles ., .. and doubled separators.
- path.join(a, b, ...): joins the parts and normalizes. Leading slashes of later parts do not reset the path: join('/srv', '/etc') gives /srv/etc. The result can be relative.
- path.resolve(a, b, ...): processes from right to left until it has an absolute path. If it never gets one, it prepends process.cwd(). A later absolute segment wins: resolve('/srv', '/etc') gives /etc. The result is always absolute.
- path.normalize(p): removes redundant separators and resolves . and .. segments in one path.
Other helpers: basename, dirname, extname, parse, format, relative, isAbsolute.
path.posix and path.win32 give the two styles on any machine. They are useful in tests, and when you must produce a path for another operating system.
Security note: join and resolve do not make a path safe. A user value like ../../etc/passwd still escapes the base folder. Check the result against the base, as shown in the path traversal question.
const path = require('node:path');
// Using the posix flavour so the output is the same on every machine.
const p = path.posix;
console.log(p.join('/srv', 'app', '../logs', 'today.txt'));
console.log(p.join('/srv', '/etc'));
console.log(p.resolve('/srv', 'app', '../logs', 'today.txt'));
console.log(p.resolve('/srv', '/etc'));
console.log(p.normalize('/srv//app/./data/../file.txt'));
console.log(p.basename('/reports/term1/marks.csv'));
console.log(p.basename('/reports/term1/marks.csv', '.csv'));
console.log(p.extname('/reports/term1/marks.csv'));
console.log(p.relative('/reports/term1', '/reports/term2/marks.csv'));
console.log(p.parse('/reports/term1/marks.csv'));
// The Windows flavour on the same machine.
console.log(path.win32.join('C:\\data', '..', 'backup', 'marks.csv'));
console.log(path.win32.basename('C:\\data\\marks.csv'));The posix and win32 objects make the output identical on any operating system. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): /srv/logs/today.txt /srv/etc /srv/logs/today.txt /etc /srv/app/file.txt marks.csv marks .csv ../term2/marks.csv { root: '/', dir: '/reports/term1', base: 'marks.csv', ext: '.csv', name: 'marks' } C:\backup\marks.csv marks.csv
Key points
- join concatenates and normalizes. resolve returns an absolute path.
- A later absolute segment resets resolve but not join.
- path.posix and path.win32 allow both styles on one machine.
- Path helpers do not protect against path traversal.
Common mistakes
- Concatenating paths with + '/' + .
- Assuming join and resolve give the same result.
- Forgetting that resolve depends on the current working directory for relative input.
- Trusting a joined user path without checking that it stays inside the base folder.
Follow-ups
- How do you get the directory of the current file in ESM?
- What does path.relative return?
- Why is path.join(__dirname, 'x') safer than 'x' alone?
Tips
- Use the 'right to left' phrase for resolve.
- Demonstrate the join('/srv', '/etc') trap.
- Link to security: normalised paths still need a base check.