Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptNode.js fs-extra ensureFile() Function

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 that directory itself. If the file already exists it will not be modified. createFile() is the other name of ensureFile() function which means we can use createFile() function in place of ensureFile() and everything will work as it is.

Syntax:

fs.ensureFile(file,callback)

or

fs.createFile(file,callback)

Parameters:

  • file: It is a string that contains the file path.
  • callback: It will be called after the task is completed by the function. It will either result in an error or success. 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. We will be passing this file in our function

index.js




// Requiring module
const fs = require("fs-extra");
  
// file path
// File already exist
// No modification
// will be done in file
const file = "file.txt";
  
// Function call
// Using callback function
fs.createFile(file, (err) => {
  if (err) return console.log(e);
  console.log("Successfully completed");
});


Output: Since we have created the file earlier the function will not create a new file and it will not modify the data our file contains.

Successfully completed

Example 2: This time we will be passing the file path which does not exist.

index.js




// Requiring module
const fs = require("fs-extra");
  
// file path
// Path contains a directory
// which does not exist
// It will create both
// directory and file
const file = "dir/file.txt";
  
// Function call
// Using Promises
fs.createFile(file)
  .then(() => console.log("Successfully Completed"))
  .catch((err) => console.log(err));


Output: You will observe that now a directory is created with the name dir and it contains a file with the name file.txt.

Successfully Completed

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments