How to create symlinks in Node.js? Symbolic link with Node.js

Introduction

symbolic link (also symlink or soft link) is a term for any file that contains a reference to another file or directory in the form of an absolute or relative path. Node.js built-in fs module provides APIs to create symlink. fs.symlink() method is used to create a symlink to the given path asynchronously. fs.symlinkSync() method is used to create a symlink to the given path synchronously. Both methods create a link making the path point to the target. The relative target is relative to the link’s parent directory.

Syntax of fs.symlink()

fs.symlink(target, path[, type], callback)

This method accept four parameters as described below:

  • target: The path to which the symlink has to be created. It can be a string, buffer or URL.
  • path: The path where the symlink will be created. It can be a string, buffer or URL.
  • type: only available on Windows and ignored on other platforms. It can be set to ‘dir’, ‘file’, or ‘junction’. If the type argument is not set, Node.js will autodetect target type and use ‘file’ or ‘dir’. If the target does not exist, ‘file’ will be used. Windows junction points require the destination path to be absolute. When using ‘junction’, the target argument will automatically be normalized to absolute path.
  • callback: It is a callback function that is called after method execution. It takes one parameter:
    • err: An error object that would be thrown if the operation fails

Following example illustrate the fs.symlink() method in Node.js

// Import the fs module
const fs = require('fs');

// function to create symbolic link.
fs.symlink("targeted_dir",
           "symlink_to_Dir", 'dir', (err) => {
  if (err)
    console.log(err);
  else {
    console.log("Symlink created");
  }
});

Syntax of fs.symlinkSync()

fs.symlinkSync(target, path[, type])

This method accept three parameters as described below:

  • target: The path to which the symlink has to be created. It can be a string, buffer or URL.
  • path: The path where the symlink will be created. It can be a string, buffer or URL.
  • type: only available on Windows and ignored on other platforms. It can be set to ‘dir’, ‘file’, or ‘junction’. If the type argument is not set, Node.js will autodetect target type and use ‘file’ or ‘dir’. If the target does not exist, ‘file’ will be used. Windows junction points require the destination path to be absolute. When using ‘junction’, the target argument will automatically be normalized to absolute path.

Following example illustrate the fs.symlinkSync() method in Node.js

// Import the fs module
const fs = require('fs');

// function to create symbolic link.
fs.symlinkSync("targeted_dir","symlink_to_Dir", 'dir);

Conclusion

It is recommended to use an asynchronous function instead of synchronous. As synchronous function blocks the event loop and should not be used in the production environment.

#Happy Coding #TopCode

#node js create symbolic link #node.js fs symbolic link #symlink node.js example symlink node.js example #symlink node nodejs #symlink with node js #node js symlink sync #node js create symlink #node js symlink directory #node.js detect symlink #node.js delete symlink #node js follow symlink #node js get symlink #node.js is symlink #node.js make symlink #node.js overwrite symlink #node js resolve symlink #node js remove symlink #symlink node to nodejs #node js symlink node.js fs module not found #node js fs module tutorial #node js fs module install #node js fs module download #node js fs module write file #node js fs module source code #node js fs module github #node js cannot find module fs #node js fs module #node js fs module example #node js fs module documentation #node js cannot resolve module ‘fs’ #fs-extra module in node js #fs module for node js #how to use node js fs module #fs module in node js #fs module in node js tutorial #fs module in node js not found #what is fs module in node js #require fs in node js #fs module in node #node js fs methods #node.js use fs module #node filesystem module

, , , , ,

Related posts

Latest posts

1 comment

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site