[英]Symbolic-Link always points to its parent directory
Placing Symbolic-Links other than the current directory does not work because they always point to their current directory放置当前目录以外的符号链接不起作用,因为它们总是指向当前目录
I'm on Windows 10, what I'm missing?我在 Windows 10,我错过了什么?
fs.symlink('./testFile', './testDir/testSymLink', function(err){ // creates a symbolic- link in the 'testDir' subfolder relative to the current directory
if(err) console.log(err);
});
fs.readlink('./testDir/testSymlink',function(err, links){ // reads the created symbolic link
if(err) console.log(err);
console.log(links); // -> '.\testFile' (points to the current directory not to the parent directory)
});
fs.readFile('./testDir/testSymlink.txt', function(err, data){ // file doesn't exist
if(err) console.log(err); // -> ENOENT no such file or directory
console.log(data); // -> undefined
});
The symbolic link is created (we can read it) but points to its current directory .\testFile
it should point to its parent directory where the reference file is ..\testFile
符号链接已创建(我们可以读取)但指向其当前目录.\testFile
它应该指向其参考文件为..\testFile
的父目录
the symlink()
method's 1st argument is the target location used by the created symbolic-link. symlink()
方法的第一个参数是创建的符号链接使用的目标位置。
It's is important to know that the symbolic-link uses the 1st argument as it was passed!重要的是要知道符号链接在传递时使用第一个参数! (does not resolve it as absolute path!) (不将其解析为绝对路径!)
Here is the tricky part, because the above 1st argument is ./testFile
the symbolic-link will use this path to reference the 'testFile' in its own directory (not in the parent directory)这是棘手的部分,因为上面的第一个参数是./testFile
符号链接将使用此路径来引用其自己目录中的“testFile”(而不是在父目录中)
Solutions:解决方案:
1: the above code could be fixed like this (the testSymLink
in the testDir
directory references the testFile
in the parent directory) 1:上面的代码可以这样固定( testSymLink
目录下的testDir
引用了父目录下的testFile
)
fs.symlink('../testFile', './testDir/testSymLink', function(err){
if(err) console.log(err);
});
2: passing absolute path as 1st argument in the symlink()
method (this will save you a lot of hassle!) 2:在symlink()
方法中将绝对路径作为第一个参数传递(这将为您节省很多麻烦!)
fs.symlink(/* absolute path */, './testDir/testSymLink', function(err){
if(err) console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.