Node.js fs-extra ensureLinkSync() Function Last Updated : 06 Oct, 2021 Comments Improve Suggest changes Like Article Like Report The ensureLinkSync() function is the synchronous version of ensureLink(). The function makes sure that the link between the two given files exists. The source file needs to exist already otherwise the function will throw an error. If the directory structure of the destination file does not exist then it will be created by the function and a link will be established between the source file and the destination file. createLinkSync() can also be used in place of ensureLinkSync(). Syntax: ensureLinkSync(srcPath, destPath) // OR createLinkSync(srcPath, destPath) Parameters: srcPath: It is a string that contains the path of the file which is to be linked with another file.destPath: It is a string that contains the path of the other file which will be linked to the file specified in srcPath. Return value: It does not return anything. Follow the steps to implement the function: The module can be installed by using the following command. npm install fs-extra After the installation of the module you can check the version of the installed module by using this command. npm ls fs-extra 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'); 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: index.js // Requiring module const fs = require("fs-extra"); // source file path // File needs to exist const srcPath = "file.txt"; // destination file path // This file exists already const destPath = "dest/file.txt"; // Function to check // if destination file // exists or not const fileExists = (path) => { if (fs.existsSync(path)) return "Destination file exists"; return "Destination file do not exists"; }; // Before function call const before = fileExists(destPath); console.log(`Before function call ${before}`); // Function Call fs.ensureLinkSync(srcPath, destPath); // After function call const after = fileExists(destPath); console.log( `After function call ${after} and Link is successfully established!!` ); Output: Example 2: JavaScript // Requiring module const fs = require("fs-extra"); // source file path // File needs to exist const srcPath = "file.txt"; // destination file path // This file do not exists // so it will be created // bt function const destPath = "destination/dest/file.txt"; // Function to check // if destination file // exists or not const fileExists = (path) => { if (fs.existsSync(path)) return "Destination file exists"; return "Destination file do not exists"; }; // Before function call const before = fileExists(destPath); console.log(`Before function call ${before}`); // Function Call fs.ensureLinkSync(srcPath, destPath); // After function call const after = fileExists(destPath); console.log( `After function call ${after} and Link is successfully established!!` ); Output: Reference: https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/ensureLink-sync.md Comment More infoAdvertise with us Next Article Node.js fs-extra ensureLinkSync() Function P pritishnagpal Follow Improve Article Tags : JavaScript Web Technologies Node.js NodeJS-function NodeJS-fs-extra +1 More Similar Reads Node.js fs-extra ensureLink() Function 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 do 3 min read Node.js fs-extra ensureDirSync() function The ensureDirSync() function is the synchronous version of ensureDir() function. The function makes sure that the directory exists, if the directory structure does not exist it will be created by the function. mkdirsSync() and mkdirpSync() can also be used in place of ensureDirSync() and the result 2 min read Node.js fs-extra ensureFileSync() function The ensureFileSync() function is the synchronous version of ensureFile() function. The function makes sure that the file exists, if the files do not exist it will be created by the function. If the requested file is in a directory that does not exist, the directory and the file inside it will be cre 2 min read Node.js fs.extra ensureDir() Function The ensureDir() function make sure that the directory user is requesting exists. If the directory structure does not exist the function will create the structure itself. mkdirs() and mkdirp() are the other names of the function which means we can use them in place of ensureDir() and everything will 2 min read Node.js fs-extra ensureFile() Function The ensureFile() function makes sure that the file user is requesting exists. If the file doesn't exist the function will create a new file. Even if the user is requesting a file that is inside some directory, but if the directory does not exist the function will create the directory and the file in 3 min read Node.js fs-extra emptyDirSync() Function The emptyDirSync() function is the synchronous version of the emptyDir() function. The function ensures that the given directory is empty. If the directory is not empty, it will delete all the content present in that directory. The directory itself is not deleted. If the directory does not exist, it 3 min read Node.js fs-extra emptyDir() Function The fs-extra is a module that adds file system methods that are not included in the native fs module. It also adds promises support to the fs method. Some file system methods are not included in the native fs module, therefore, they have to be installed separately if we need to use them but the fs-e 3 min read Node.js fs-extra move() Function The move() function moves a file or a directory from source to the destination specified by the user. If you want to move a file to a folder in which a file with the same name already exists, the function will overwrite the file if we have set the option of overwrite to true else it will throw an er 3 min read Node.js fs.copyFileSync() Function The fs.copyFileSync() method is used to synchronously copy a file from the source path to destination path. Node.js will overwrite the file if it already exists in the destination. The optional mode parameter can be used to specify the behavior of the copy operation. Syntax:  fs.copyFileSync(src, 3 min read Node.js fs-extra remove() Function the remove() function deletes the given file or directory. All the files inside a directory are deleted. If the given file or directory does not exist the function will do nothing. Syntax: fs.remove(path,callback) Parameters: This function accepts two parameters as mentioned above and described belo 1 min read Like