Node.js – Create File using Node FS

We can create a new file in Node.js using Node fs module.

In this tutorial, we shall learn to create a File in Node.js using Node FS (File System) built-in module. Node.js example programs that use writeFile(), appendFile() or open() function are provided.

Syntax – writeFile() function

The syntax of fs.writeFile() function is

fs.writeFile('<fileName>',<contenet>, callbackFunction)

A new file is created with the specified name. After writing to the file is completed (could be with or without error), callback function is called with error if there is an error reading file.

If a file already exists with the name, the file gets overwritten with a new file. Care has to be taken while using this function, as it overwrites existing file.

ADVERTISEMENT

Syntax – appendFile() function

The syntax of fs.appendFile() function is

fs.appendFile('<fileName>',<contenet>, callbackFunction)

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

Syntax – open() function

The syntax of fs.open() function is

fs.open('<fileName>',<file_open_mode>, callbackFunction)

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

Steps – Create a File in Node.js

Following is a step by step guide to create a new File in Node.js :

Step 1 : Include File System built-in module to your Node.js program

var fs = require('fs');

Step 2 : Create file using one the methods mentioned above. Following are the examples demonstrating to create a file in Node.js with each of these methods.

Example 1 – Create File using writeFile()

In this example, we will use writeFile() function and create a file named newfile.txt.

createFileExample.js

// include node fs module
var fs = require('fs');

// 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 node command in terminal or command prompt.

Output

$ node createFileExample.js
File is created successfully.

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

Example 2 – Create File using appendFile()

In this example, we will use appendFile() function and create a file named newfile_2.txt.

createFileExample2.js

// include node fs module
var fs = require('fs');

// 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 node command in terminal or command prompt.

Output

$ node createFileExample2.js
File is created successfully.

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

Example 3 – Create File using open()

In this example, we will use open() function and create a file named newfile_3.txt.

createFileExample3.js

// include node fs module
var fs = require('fs');

// open function with filename, file opening 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 node command in terminal or command prompt.

Output

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

The file should be opened in write mode.

Conclusion

In this Node.js Tutorial – Node FS, we have learnt to create a File in Node.js using Node FS (File System) module.