Write a file into specific folder in Node JS example


Hi friends, in this tutorial you will learn how to write a file into specific folder in node js and it is a very basic concept of a node js filesystem. In order to write a file you have to follow the below steps:

Required steps to write a file into specific folder in Node JS

Step 1:- Create a folder inside the node js application or anywhere you want.

Step 2:- Create a file with a .js extension inside the node js application. For eg:- filesystem.js

Also read, Node Js hello world program

Now, before writing a file, you have to include two built-in modules of node js as given below

fs module:- In order to work with the filesystem such as creating a file, reading a file, updating the file, and deleting a file in node js, there is a built-in module called “fs”. You can use this module with the help of require() method as shown below.

const fs = require("fs");

path module:- In order to get the path of a specific folder of the directory, you can include this module as shown below.

const path = require("path");

Step 3:- In the filesystem.js file, call the above two modules as given below.

const fs = require('fs');
const path = require('path');

Step 4:- Now, get the path of the specific folder where you want to write the file using the path variable with the help of the join() method as shown below.

const directory = path.join(__dirname,'your folder name');

Now, if you console the above directory variable and run your node js file in the terminal then you can see the folder path as shown below

D:\nodejs_tutorials\myfiles

Step 5:- Next, we will create the file using the writeFileSync() function with the help of filesystem constant “fs“. as given below.

fs.writeFileSync(directory+"/test.txt","Hi this is a text file");

Note that in the above line of code, do not forget to include the directory inside the writeFileSync() function because the file will be created inside that specific directory.

Also, read, How to install package JSON file in node js

Step 6:- Now, in the VS code, open the terminal and run the node js file (filesystem.js) and you will see that the test.txt file has been created under the specific folder you mentioned at the time of directory declaration.

Complete Code:- (filesystem.js)

const fs = require('fs');
const path = require('path');
const directory = path.join(__dirname,'myfiles');
//console.log(directory);
fs.writeFileSync(directory+"/test.txt","Hi this is a text file");

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.


Leave a Comment

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

RATU311

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

THOR311

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311