Open In App

Node.js fs-extra ensureLink() Function

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The ensureLink() function ensures that the link between the given two files exists. If the destination file doesn’t exist it will be created and a link between them is created but if the source file doesn’t exist the function will throw an error. If the directory structure of the destination file doesn’t exist it will be created. By link, it is meant that both the linked files will be replicas of each other. If anything is changed in one file it will be reflected in the other file as well. The createLink() is the other name of the function.

Syntax:

fs.ensureLink(srcPath,destPath,callback)
fs.createLink(srcPath,destPath,callback)

Parameters: This function accepts three parameters as mentioned above and described below.

  • srcPath: It is a string that contains the path of the file which is to be linked with another file. This file needs to exist otherwise the function will throw an error.
  • destPath: It is a string that contains the path of the other file which will be linked to the file specified in srcPath.
  • callback: It will be called after the link is established or if some error occurred during execution. Promises can also be used in place of the callback function.

Return value: It does not return anything.

Follow the steps to implement the function:

  1. The module can be installed by using the following command:

    npm install fs-extra
  2. After the installation of the module you can check the version of the installed module by using this command:

    npm ls fs-extra

  3. Create a file with the name index.js and require the fs-extra module in the file using the following command:

    const fs = require('fs-extra');
  4. To run the file write the following command in the terminal:

    node index.js

Project Structure: The project structure will look like this.

Example 1: Create a file with the name file.txt and a folder with name des and create a file with the name file.txt inside the folder. We will be trying the function on the files which already exist.

index.js




// Requiring module
const fs = require("fs-extra");
  
// source file path
// File needs to exist
const src = "file.txt";
// destination file path
const dest = "des/file.txt";
  
// Function Call
// Using callback function
fs.ensureLink(src, dest, (err) => {
  if (err) return console.log(err);
  console.log("Success! Both files are now linked");
});


Output: This output will be in console. You will observe that the link is established between both the files and now if we write anything in one file it will be reflected in the other.

Example 2: This time we will only be creating the source file. The function itself will create the destination file and will establish links between them.

index.js




// Requiring module
const fs = require("fs-extra");
  
// source file path
// File needs to exist
const src = "file.txt";
// destination file path
// File will be created if not exist
const dest = "des/file.txt";
  
// Function Call
// Using Promises
fs.ensureLink(src, dest)
  .then(() => console.log("Success!! Links between files established"))
  .catch((e) => console.log(e));


Output: This output will be in console. You will observe that first a directory is created with the name des and a file with the name file.txt is created and then a link is established in both the files.

Note: In both above cases the output will look like. In the above output, you can see that if anything is changed in one file it will be reflected in the other. So this is how we can use ensureFile() function.

Reference: https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/ensureLink.md



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads