Node.js symlink junctions are broken on Windows?

2019-06-09 20:55发布

问题:

First, here's the code:

const FS = require('fs');
const OS = require('os');
const symlinkType = OS.platform() === 'win32' ? 'junction' : 'file';

FS.symlink(target, path, symlinkType, err => {
    if(err) {
        console.error(`Failed to create ${symlinkType} ${path} -> ${target}`);
    } else {
        console.log(`Created ${symlinkType} ${path} -> ${target}`);
    }
})

This spits out a bunch of messages like this:

Created junction C:\Users\Mark\*snip*\data\Pacific\Midway.txt -> C:\Users\Mark\*snip*\data\Pacific\Pago_Pago.txt
Created junction C:\Users\Mark\*snip*\data\Pacific\Samoa.txt -> C:\Users\Mark\*snip*\data\Pacific\Pago_Pago.txt
Created junction C:\Users\Mark\*snip*\data\Pacific\Ponape.txt -> C:\Users\Mark\*snip*\data\Pacific\Pohnpei.txt

So it looks like it's working. I can see the junctions/shortcuts in Explorer:

But they're all broken. i.e., double clicking them gives me an error message

Cuba.txt is not accessible.
The filename, directory name, or volume label syntax is incorrect.

How come? How can I create symlinks in Node.js on Windows such that the files work like normal (i.e. other programs can read them).

回答1:

As I remember NTFS junction points act like directories, not individual files.

See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365006(v=vs.85).aspx