Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 903
cc young Avatar asked Jan 07 '13 10:01

cc young


People also ask

How do I remove a symbolic link in npm?

You can “undo” the effects of npm link by simply removing the symbolic links. But there is a built in command for it, aptly called: npm unlink . Just run npm unlink --no-save <module_name> on your project's directory to remove the local symlink, and run npm unlink on the module's directory to remove the global symlink.

How do I stop symbolic links?

Conclusion. To remove a symbolic link, use either the rm or unlink command followed by the name of the symlink as an argument. When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.

Can you edit a symlink?

No. The symlink system call will return EEXIST if newpath already exists. You can only link from a new node in the filesystem.

Can you chain Symlinks?

Yes. You can only stack so much symbolic links together before the kernel and/or application refuse to follow the chain.


Video Answer


2 Answers

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

like image 147
Marco Carvalho Avatar answered Oct 19 '22 23:10

Marco Carvalho


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.

like image 41
Diego Avatar answered Oct 19 '22 22:10

Diego