How to Create Text File in Specific Directory in Node.js?

Aug 30, 2022

.

Admin

How to Create Text File in Specific Directory in Node.js?

Hi friends

This tutorial is focused on how to create a text file in a specific directory in node.js. Here you will learn to create a text file in a specific directory in nodejs. if you have question about create text file in specific directory in node js example then I will give simple example with a solution. I would like to share with you node js create a text file in a specific directory. Here, Creating a basic example of node.js create a text file in a specific directory.

In this tutorial, we shall learn to create text file in specific directory in node.js using Node FS (File System) built-in module. Node.js example programs that use writeFile() function are provided. So, let's follow a few step to create an example of node js create text file specific directory example.

Let's see below simple example with output:

Step 1: Create Node.js Project


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Update server.js File

server.js

// require fs (File System) create file

fs = require('fs')

// create text file in specific directory

fs.writeFile('readme.txt', 'Hi Welcome to Nicesnippets.com', function (err) {

if (err) throw err;

console.log('Results Received');

});

Output:

Results Received

It will update text readme.txt file.

readme.txt

Hi Welcome to Nicesnippets.com

I hope it can help you...

#Node JS