How to Create Text File in Node.js?
Hi friends,
This article goes into detail on how to create a text file in node.js. you will learn how to create a text file in nodejs. I’m going to show you about create a text file in node.js. if you have a question about create a text file in nodejs then I will give a simple example with a solution. So, let's follow a few step to create an example of node js create text file example.
In this tutorial, we shall learn to create text File in Node.js using Node FS (File System) built-in module. Node.js example programs that use writeFile() function are provided. I will give a simple example of create a text file in nodejs.
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
const fs = require('fs');
const data = "This is the new content of the file from NiceSnippets.com.";
fs.writeFile('readme.txt', data, (err) => {
if(err) {
throw err;
}
console.log("New Text File Successfully.");
})
Output:
readme.txt
This is the new content of the file from NiceSnippets.com.
- How to Install Node Js and NPM Ubuntu?
- How to used Google ReCaptcha V3 in Node Js Express?
- How to Send Email with Attachment in Node js?
- How to Insert Data from Form into MySQL Database using Node js Express?
- Install Node Js and NPM Ubuntu 18.04/20.04/22.04 Tutorial
- How to Delete Data by id into MongoDB in Node JS?
- Install Node.js 16/17/18 on CentOS 8 Tutorial
- How to Upload File in MongoDB using Node js Express?