8

Note: using Node.js 8

I have a series of symlinks: a -> b -> c

I need to resolve the initial symlink a to its target destination b. How can this be accomplished in Node.js?

The fs.realpath function resolves chains of symlinks, so it resolves a to c. This is not the desired behavior.

I've also attempted to find an npm package to do this, but haven't had any luck so far.

I thought maybe I could fs.open the symlink and read the contents, but I could not figure out how to access the documented fs.constants.O_SYMLINK constant, probably because I'm on Node 8.

4
  • Node 8 documentation also lists fs.constants.O_SYMLINK. What is there to figure out? If you type that into the REPL, doesn't it give you a numeric value? Sep 6, 2018 at 23:20
  • No, fs.constants.O_SYMLINK is undefined.
    – rich remer
    Sep 11, 2018 at 23:44
  • What does node -v output? Sep 12, 2018 at 0:07
  • node -v outputs v8.10.0
    – rich remer
    Sep 12, 2018 at 1:48

1 Answer 1

14

I searched Node.js documentation for the word "symlink", but the Node documentation just refers to these as a "link". The solution is to use fs.readlink():

const {readlink} = require("fs");

fs.readlink("a", (err, target) => {
    if (!err) console.log(target);    // prints "b"
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.