2

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

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • Hardlinks (`fs.link`) see to work just fine. Windows shows no indication that the file *is* a hardlink, but editing one of the linked files and then looking at another shows that they all point to the same data. – mpen Mar 27 '17 at 02:28
  • On Windows, a shortcut is not a symlink so I presume a symlink is not a shortcut (yes, symlinks actually exist on NTFS). Instead of double clicking try cd'ing to it – slebetman Mar 27 '17 at 03:12
  • @slebetman They're actually files, not directories. Can't `cd` into it. – mpen Mar 27 '17 at 04:58

1 Answers1

4

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

Burt_Harris
  • 6,415
  • 2
  • 29
  • 64