Skip to content
jshacks.dev
Node.js

fs.promises.readlink() in Node.js Explained With Examples

Read where a symbolic link points with fs.promises.readlink in Node.js 24 and 26, resolve relative targets, tell links from files and handle EINVAL.

8 min read
Summarize in AI
Flat illustration of an editor window full of code, with the Node.js, JavaScript, TypeScript and Vite logos.

A symbolic link is a small file whose content is a path to somewhere else. Most file functions follow the link without telling you, so readFile on a link returns the content of the target and stat describes the target. Sometimes you need the link itself: where does it point, is the target relative or absolute, is the link broken? fs.promises.readlink answers exactly that. It reads the stored target text of a link and does not touch what it points to. This post covers readlink from node:fs/promises on Node.js 24 and 26: the basic call, relative versus absolute targets, resolving a target to a full path, detecting links with lstat, broken links, the EINVAL error on ordinary files and the Windows caveats. It pairs with symlink, which creates links, and lstat. Each sample builds its own temporary folder, so the outputs are exact apart from the temporary path, which the samples avoid printing.

The basic call

readlink(path[, options]) resolves with the link's target exactly as it was stored.

basic.mjsJavaScript
import { mkdtemp, writeFile, symlink, readlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
await writeFile(join(dir, "real.txt"), "hello");
await symlink("real.txt", join(dir, "alias.txt"));

console.log(await readlink(join(dir, "alias.txt")));
Text
real.txt

The result is the text real.txt, not a full path. The link was created with a relative target, and readlink returns it unchanged. It does not check that the target exists, and it does not resolve anything: it only reads what is stored in the link.

Relative and absolute targets

A link target can be relative to the folder holding the link, or absolute. readlink shows which one you have.

kinds.mjsJavaScript
import { mkdtemp, writeFile, symlink, readlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, isAbsolute } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
const real = join(dir, "real.txt");
await writeFile(real, "x");

await symlink("real.txt", join(dir, "relative"));
await symlink(real, join(dir, "absolute"));

console.log(isAbsolute(await readlink(join(dir, "relative"))));
console.log(isAbsolute(await readlink(join(dir, "absolute"))));
Text
false
true

This matters when you move or copy a tree. A relative link keeps working if the folder moves as a whole, because the target is found from the link's own location. An absolute link keeps pointing at the old place and breaks. Deployment scripts that publish a current link to the latest release usually want to know which kind they are dealing with.

Turning the target into a full path

A relative target is relative to the folder that contains the link, not to the current working directory. That is the most common bug with readlink. Resolve it against the link's folder.

resolve.mjsJavaScript
import { mkdtemp, mkdir, writeFile, symlink, readlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, dirname, resolve, relative } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
await mkdir(join(dir, "releases"));
await mkdir(join(dir, "app"));
await writeFile(join(dir, "releases", "v2.txt"), "v2");
await symlink("../releases/v2.txt", join(dir, "app", "current"));

const linkPath = join(dir, "app", "current");
const target = await readlink(linkPath);
const full = resolve(dirname(linkPath), target);

console.log(target);
console.log(relative(dir, full));
Text
../releases/v2.txt
releases/v2.txt

Calling resolve(target) alone would have used the working directory and produced a wrong path. Using dirname(linkPath) as the base gives the true location. If you only want the final real path with every link followed, fs.promises.realpath does that in one step, but it fails when anything along the way is missing, while readlink plus resolve lets you inspect a broken link.

Use lstat, which describes the link itself, to find out whether a path is a link before you read it. Plain stat follows the link and would describe the target.

detect.mjsJavaScript
import { mkdtemp, writeFile, symlink, lstat, stat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
await writeFile(join(dir, "file.txt"), "x");
await symlink("file.txt", join(dir, "alias.txt"));

console.log((await lstat(join(dir, "alias.txt"))).isSymbolicLink());
console.log((await stat(join(dir, "alias.txt"))).isSymbolicLink());
console.log((await lstat(join(dir, "file.txt"))).isSymbolicLink());
Text
true
false
false

lstat reported the link, and stat reported the file it points to, so isSymbolicLink() is false there. When you list a folder with readdir and withFileTypes: true, each Dirent has isSymbolicLink() as well, which avoids a call per entry.

Calling readlink on something that is not a symbolic link fails with EINVAL, meaning the argument is not valid for this operation.

einval.mjsJavaScript
import { mkdtemp, writeFile, readlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
await writeFile(join(dir, "plain.txt"), "x");

try {
  await readlink(join(dir, "plain.txt"));
} catch (err) {
  console.log(err.code);
}

try {
  await readlink(join(dir, "missing"));
} catch (err) {
  console.log(err.code);
}
Text
EINVAL
ENOENT

A regular file gives EINVAL, and a path that does not exist at all gives ENOENT. A script that handles "this might be a link or a file" can catch EINVAL and treat it as "not a link", though checking with lstat first is easier to read.

A link whose target is gone is still a link, and readlink still reads it. This is the case where readlink shines, because stat and readFile would fail with ENOENT.

broken.mjsJavaScript
import { mkdtemp, symlink, readlink, stat, lstat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
const link = join(dir, "dangling");
await symlink("gone.txt", link);

console.log(await readlink(link));
console.log((await lstat(link)).isSymbolicLink());

try {
  await stat(link);
} catch (err) {
  console.log(err.code);
}
Text
gone.txt
true
ENOENT

Tools that clean up or audit a folder use this combination: find links with lstat, read the target with readlink, and test whether the target exists with stat to report the dangling ones.

The encoding option

The optional second argument is an encoding string or an object with encoding. The default is "utf8", so the result is a string. Passing "buffer" gives a Buffer, which is only needed when a target contains bytes that are not valid text. Everyday code leaves this alone.

Windows notes

On Windows, links come in several kinds: file links, directory links and junctions, and creating them may need special permission. readlink works on them, but the form of the result can differ from what you passed to symlink: a junction target is typically returned as an absolute path, and some targets may carry a \\?\ prefix. If your code compares a target string with an expected string, normalise both with path.resolve first. Code that must run on both systems should treat the returned text as a path to resolve, not as something to compare verbatim.

Common mistakes

  • Resolving a relative target against the working directory instead of the link's folder.

  • Expecting a full path. The result is exactly what was stored.

  • Calling readlink on regular files and not handling EINVAL.

  • Using stat to check for a link, which follows it. Use lstat.

  • Assuming the target exists. Test it separately.

  • Comparing target strings across operating systems without normalising them.

A common release layout keeps each version in its own folder and a current link pointing at the live one. To report which version is live, read the link and take the last part of the target. To roll back, create a new link to the previous version and rename it over current, since a rename is atomic and a reader never sees a missing link. readlink is the read side of that design: it lets a status command say "live version is v2" without parsing folder timestamps or guessing.

version.mjsJavaScript
import { mkdtemp, mkdir, symlink, readlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, basename } from "node:path";

const dir = await mkdtemp(join(tmpdir(), "link-"));
await mkdir(join(dir, "releases", "2026-11-20"), { recursive: true });
await symlink(join("releases", "2026-11-20"), join(dir, "current"));

console.log(basename(await readlink(join(dir, "current"))));
Text
2026-11-20

Because the target was stored relative, the whole dir can be moved or copied with links preserved and the status check keeps working.

Auditing a folder for broken links is the clearest everyday use of readlink. Walk the folder with readdir and withFileTypes, keep the entries where isSymbolicLink() is true, read each target, resolve it against the entry's folder and test whether it exists with stat. Report every link whose target is missing, along with the stored text, so whoever reads the report can see whether the link was meant to be relative. Do the checks one folder at a time and avoid following links into other trees, because a cycle of links can send a naive walker around forever. When you only need the final location of a working link, realpath is shorter, since it follows every hop and returns one absolute path. When the question is what a link says rather than where it ends, which is the situation for deployment tools, backup tools and package managers, readlink is the correct call. Keep in mind that the result is a snapshot: another process can replace the link a moment later, so a decision based on it should be made close to the action, and the action itself should tolerate failure. If a script must be robust against that race, perform the change with an atomic rename rather than checking and then acting.

Also inAsync
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.