English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File(FS)

NodeJS Other

Node.js Create File in FS

We will learn to create files in Node.js using the built-in Node FS (File System) module. Node.js instance programs, using the features provided by writeFile(), appendFile(), or open().

Create a file in Node.js

The following is a step-by-step guide to creating a new file in Node.js:

  • Step1Step: Include the File System built-in module in the Node.js program

     var fs = require(‘fs‘);
  • Steps2: Create a file using one of the following methods

    • writeFile() function

       fs.writeFile(‘<fileName>’,<contenet>,callbackFunction)

      A new file will be created using the specified name. After the file is written (there may or may not be an error), if an error occurs while reading the file, the callback function with the error will be called. If the name already exists, the file will be overwritten by the new file. Caution must be exercised when using this feature, as it will overwrite existing files (if any).

    • appendFile() function

       fs.appendFile(‘<fileName>’,<contenet>,callbackFunction)

      If the file specified in the appendFile() function does not exist, a new file will be created and the content will be passed to the function.

    • open() function

       fs.open(‘<fileName>’,<file_open_mode>,callbackFunction)

      If the specified file is not found, a new file will be created with the specified name and mode, and it will be sent to the callback function.

Example

writeFile() – Create the following Node.js program to create a file in Node.js

// Introduce the fs module
var fs = require('fs'); 
 
// The writeFile function with filename, content, and callback function
fs.writeFile('newfile.txt', 'Learn Node FS module', function (err) { 
  if (err) throw err; 
  console.log('File is created successfully.'); 
 });

Run the program using the node command in the terminal or command prompt:

Terminal Output

$ node createFileExample.js
File is created successfully.

The file should be created next to the example node.js program with the content "Learn Node FS module".

appendFile() – Create the following Node.js program to create a file in Node.js

// Introduce the fs module
var fs = require('fs'); 
 
// The appendFile function with filename, content, and callback function
fs.appendFile('newfile_2.txt', 'Learn Node FS module', function (err) { 
  if (err) throw err; 
  console.log('File is created successfully.'); 
 });

Run the program using the node command in the terminal or command prompt:

Terminal Output

$ node createFileExample2.js
File is created successfully.

The file should be created next to the example node.js program with the content "Learn Node FS module".

open() – Create the following Node.js program to create a file in Node.js

// Introduce the fs module
var fs = require('fs'); 
 
// Open with filename, file open mode, and callback function
fs.open('newfile_3.txt', 'w', function (err, file) { 
  if (err) throw err; 
  console.log('File is opened in write mode.'); 
 });

Run the program using the node command in the terminal or command prompt:

Terminal Output

$ node createFileExample3.js
File is opened in write mode.

The file should be opened in write mode.

Summary:

In this Node.js tutorial-In this Node.js tutorial, we learned how to create files in Node.js using the Node FS (File System) module.