path.resolve()

Resolves a sequence of path segments into an absolute path.

Signature
path.resolve([...paths])
  • Added in Node.js v0.3.4

Parameters

ParameterTypeDescription
$...pathsoptionalstringA sequence of paths or path segments. Zero-length segments are ignored.

Return value

stringAn absolute path built by processing the segments right to left, falling back to the current working directory.

Errors

Throws a TypeError (ERR_INVALID_ARG_TYPE) if any argument is not a string.

path.resolve() processes its arguments from right to left, prepending each one until it has built an absolute path. The moment a segment is absolute, everything to its left is discarded. If the arguments are exhausted without producing an absolute path, the current working directory is prepended.

Think of it as simulating a sequence of cd commands: path.resolve('/a/b', 'c', '/d', 'e') is '/d/e', because the cd /d in the middle throws away where you had been.

When to reach for it

Reach for it whenever a path must be absolute — anchoring a root directory at startup, canonicalising a path from a config file or CLI argument before touching the filesystem, or normalising two paths so they can be compared for equality.

What trips people up

The result depends on process.cwd(), which is whatever directory the process was launched from, not the directory containing your source file. A script that works when run from the project root breaks when run from elsewhere. For a path relative to the current module, use __dirname (CommonJS) or import.meta.dirname (ESM) as the first argument.

Trailing separators are stripped from the result, except on a root path, and the output is always normalized.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Turning a CLI-supplied relative path into an absolute one before opening it
  • Anchoring an upload root at startup so later requests can be checked for containment
  • Comparing two paths for equality by resolving both and comparing the strings

Frequently asked questions

How do I get the absolute path of a file in Node.js?

Call path.resolve() with the relative path: path.resolve('data/file.txt') returns it joined onto process.cwd(). To anchor on the source file instead of the launch directory, pass __dirname first in CommonJS, or import.meta.dirname in ES modules.

Why does path.resolve give a different result depending on where I run the script?

Because relative segments are resolved against process.cwd(), the directory the process was started in, not the directory of the file containing the call. Pass __dirname or import.meta.dirname as the first argument to make the result independent of the launch directory.

Does path.resolve check that the file exists?

No. It is pure string manipulation and never touches the filesystem, so it happily resolves paths to files that do not exist. It also does not follow symlinks — use fs.realpath() or fs.realpathSync() if you need the canonical on-disk path.

More examples

Right-to-left processing stops at the first absolute segment

Using path.posix keeps the output identical on every platform. The first line shows the '/d' argument discarding '/a/b' and 'c' entirely; the last shows the trailing slash being removed, which is why resolved paths compare cleanly with ===.

Example · javascript
const path = require('node:path');

console.log(path.posix.resolve('/a/b', 'c', '/d', 'e'));
// -> /d/e

console.log(path.posix.resolve('/foo/bar', './baz'));
// -> /foo/bar/baz

console.log(path.posix.resolve('/foo/bar', '/tmp/file/'));
// -> /tmp/file   (trailing separator stripped)

Confining a resolved path to a root directory

resolve() gives an absolute candidate, and relative() reports how to get there from the root. A relative result that starts with '..' means the candidate is outside the root, which is exactly the third case. This resolve-then-relative pair is the standard containment check.

Example · javascript
const path = require('node:path');

const root = path.resolve('uploads');

function check(userInput) {
  const target = path.resolve(root, userInput);
  const rel = path.relative(root, target);
  const inside = rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
  return { rel: rel || '.', inside };
}

console.log(check('avatars/me.png'));
console.log(check('./a/../b.txt'));
console.log(check('../../etc/passwd'));

Resolving relative to the module rather than the cwd

The two forms agree only when the script happens to be run from its own directory. Anchoring on __dirname makes the path independent of where the process was launched, which is what you almost always want for files shipped alongside your code.

Example · javascript
const path = require('node:path');

const fromCwd = path.resolve('config.json');
const fromModule = path.resolve(__dirname, 'config.json');

console.log('same result:', fromCwd === fromModule);
console.log('cwd-based basename:   ', path.basename(fromCwd));
console.log('module-based basename:', path.basename(fromModule));

Discussion

  • Be the first to comment on this lesson.