Symbolic
放置当前目录以外的符号链接无效,因为它们始终指向其当前目录
我在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
});
创建了符号链接(我们可以读取它),但是指向其当前目录.\testFile
,它应该指向其父目录,其中参考文件为..\testFile
symlink()
方法的第一个参数是所创建的符号链接使用的目标位置。
重要的是,符号链接在传递时使用第一个参数! (不会将其解析为绝对路径!)
这里是棘手的部分,因为上面的第一个参数是./testFile
,符号链接将使用此路径在其自己的目录(而不是父目录)中引用'testFile']
解决方案:
1:上面的代码可以像这样固定(testSymLink
目录中的testDir
引用父目录中的testFile
)
fs.symlink('../testFile', './testDir/testSymLink', function(err){ if(err) console.log(err); });
2:在
symlink()
方法中将绝对路径作为第一个参数传递(这将为您节省很多麻烦!)
fs.symlink(/* absolute path */, './testDir/testSymLink', function(err){
if(err) console.log(err);
});