简体   繁体   English

Node.js确定symlink定位的路径

[英]Node.js determine path targeted by symlink

Note: using Node.js 8 注意:使用Node.js 8

I have a series of symlinks: a -> b -> c 我有一系列的符号链接: a - > b - > c

I need to resolve the initial symlink a to its target destination b . 我需要将初始符号链接a解析为其目标目的地b How can this be accomplished in Node.js? 如何在Node.js中完成?

The fs.realpath function resolves chains of symlinks, so it resolves a to c . fs.realpath函数解析符号链接链,因此将a解析为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. 我也试图找到一个npm软件包来做到这一点,但是到目前为止还没有运气。

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. 我以为也许可以fs.open打开符号链接并阅读内容,但是我无法弄清楚如何访问已记录的fs.constants.O_SYMLINK常量,可能是因为我在Node 8上。

I searched Node.js documentation for the word "symlink", but the Node documentation just refers to these as a "link". 我在Node.js文档中搜索了“ symlink”一词,但Node文档只是将它们称为“链接”。 The solution is to use fs.readlink() : 解决方案是使用fs.readlink()

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM