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
?
In general, no. Technically, there will be a very slight performance hit for the indirection, but it won't be noticeable to your application. As an example, most shared libraries are symlinks to symlinks (e.g. libQtCore.so -> libQtCore. so.
If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file.
When you remove a symlink, the file it points to is not affected. Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.
We can use the -l option of rsync for copying symlinks. rsync copies the symlinks in the source directory as symlinks to the destination directory using this option. Copying the symlinks is successful in this case.
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."); });
Read the full documentation on the GitHub repository
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With