Search code examples
node.jswindowsjunction

Node.js symlink junctions are broken on Windows?


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).


Solution

  • 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