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