Write in a file using node js

share link

by shivanisanju03 dot icon Updated: Mar 31, 2023

technology logo
technology logo

Guide Kit Guide Kit  


The fs module in Node.js is used in writing the file in Node.js. The file can be read and written in both Synchronous and Asynchronous ways in node.js 

fs.writeFile() API allows us to write to files in node.js easily. The synchronous version fs.writeFileSync() can also be used alternatively. 

Promised-based fsPromises.writeFile() method, offered by the fs/promises module, can also be used. 

Below is an example of how to write in files using node js. 

Fig: Preview of the output that you will get on running this code from your IDE.



Follow the steps carefully to get the output easily.

  1. Install Visual Studio Code on your computer.
  2. create a new file using .js extension
  3. Copy and paste the code
  4. Save and rum the code to get output in the output terminal
  5. The writable file will appear left side below .js file
  6. Open the file and view the written file

I found this code snippet by searching for ' Write in a file using node.js ' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created and tested in Visual Studio Code 1.74.1.

Using this solution, we are able to create folder files using node js with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to create folder files using node js


  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.