in node.js, cannot get rid of a bad symlink

I have an uglify function that creates a file lib-0.1.4-min.js and then symlinks that to lib-production-min.js. 0.1.4 is the current version.

due to synchronization of this directory, sometimes the lib-production-min.js is a broken link.

when I run the compile function, fs.existsSync( "lib-production-min.js" ) returns false. when I try to create the symlink later, node errs out with file already exists.

var version = 'lib-0.1.4-min.j';
var prod = 'lib-production-min.js';

// if production exists, get rid of it
if( fs.existsSync(prod) ) fs.unlinkSync( prod );  // not exists - not deleted

// link version to production
fs.symlinkSync( version, prod );                  // ERROR: file already exists
  1. how do I check if this deadlink is in the directory?

  2. will normal fs.unlinkSync( "lib-production-min.js" ) delete it?

fs.lstat() or fs.lstatSync() might help you. They are supposed to bring the information about the link itself, not following it.

Use fs.readlinkSync(symlinkPath) to get the file pointed by the symlink, and then use fs.existsSync with that path.

The problem is that the link file exists, is the destination of the link the one that is missing.