I use the fs
module to create symlinks.
fs.symlink("target", "path/to/symlink", function (e) {
if (e) { ... }
});
If the path/to/symlink
already exists, an error is sent in the callback.
How can I force symlink creation and override the existing symlink?
Is there another alternative than check error + delete existing symlink + try again
?
When using the ln
command line tool we can do this using the -f
(force) flag
ln -sf target symlink-name
However, this is not possible using the fs
API unless we implement this feature in a module.
I created lnf
- a module to override existing symlinks.
// Dependencies
var Lnf = require("lnf");
// Create the symlink
Lnf.sync("foo", __dirname + "/baz");
// Override it
Lnf("bar", __dirname + "/baz", function (err) {
console.log(err || "Overriden the baz symlink.");
});