Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
ensureSymlink() function in fs-extra - NodeJS
Introduction to Async ensureSymlink()
This method will ensure whether the symlink exists or not. It will create the directory structure if it does not exist.
Syntax
createSymlink(srcPath, destPah[, type] [, callback])
Parameters
srcPath – The source path of the file.
destPath – Destination path of the file.
type – This parameter is only available on Windows and ignored on other platforms.The possible values for this parameter is dir, file or junction.
callback – This function will give a callback if any error occurs.
Example
Check that fs-extra is installed before proceeding; if not, install fs-exra.
You can use the following command to check whether fs-extra is installed or not.
npm ls fs-extra
Create a createSymlink.js and copy-paste the following code snippet into that file.
Now, run the following command to run the following code snippet.
node createSymlink.js
const fs = require('fs-extra')
const srcPath = '/tmp/file.txt'
const destPath = '/tmp/dest/file.txt'
// Creating Symlink with a callback:
fs.ensureSymlink(srcPath, destPath, err => {
console.log(err) // This will return null.
// Successfully created the symlink
})
// Creating Symlink with Promises:
fs.ensureSymlink(srcPath, destPath)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// Creating Symlink with async/await:
async function createSymlinkExample (src, dest) {
try {
await fs.ensureSymlink(src, dest)
console.log('Success!')
} catch (err) {
console.error(err)
}
}
createSymlinkExample(srcPath, destPath)
Output
C:\Users\tutorialsPoint\> node createSymlinkExample.js
null
{ [Error: EEXIST: file already exists, symlink '/tmp/file.txt' ->
'/tmp/dest/file.txt']
errno: -17,
code: 'EEXIST',
syscall: 'symlink',
path: '/tmp/file.txt',
dest: '/tmp/dest/file.txt' }
{ [Error: EEXIST: file already exists, symlink '/tmp/file.txt' ->
'/tmp/dest/file.txt']
errno: -17,
code: 'EEXIST',
syscall: 'symlink',
path: '/tmp/file.txt',
dest: '/tmp/dest/file.txt' }
Introduction to ensureSymlinkSync()
This method also ensures that the symlink exists. It creates the directory structure if does not exist.
Syntax
createSymlinkSync(srcPath, destPath[, type])
Parameters
srcPath – The source path of the file.
destPath – Destination path of the file.
type – This parameter is only available on Windows and ignored on other platforms. The possible values for this parameter is dir, file or junction.
Example
Check that fs-extra is installed before proceeding; if not, install fs-exra.
You can use the following command to check whether fs-extra is installed or not.
npm ls fs-extra
Create a createSymlinkSyncExample.js and copy-paste the following code snippet into that file.
Now, run the following command to run the following code snippet.
node createSymlinkSyncExample.js
Code Snippet−
const fs = require('fs-extra')
const srcPath = '/tmp/file.txt'
const destPath = '/tmp/dest2/file.txt'
fs.ensureSymlinkSync(srcPath, destPath)
console.log('Success... !')
Output
C:\Users\tutorialsPoint\> node createSymlinkSyncExample.js Success... !