How to Upload File using formidable module in Node.js ?
Last Updated :
17 Oct, 2024
A formidable module is used for parsing form data, especially file uploads. It is easy to use and integrate into your project for handling incoming form data and file uploads.
Approach
To upload file using the formidable module in node we will first install formidable. Then create an HTTP server to accept form submissions, use formidable to parse incoming file data, handle file upload, and store it in the desired directory on the server.
Step to Upload file using Formidable in Node.js
Step 1:Â Initializing the Node App
Use the below command to create a new node app
npm init -y
Step 2:Â Installing the formidable module
You can visit the link Install formidable module. You can install this package by using this command.
npm install formidable
After installing the formidable module, you can check your yards version in the command prompt using the command.
npm version formidable
Step 3: Run the Application
After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
node index.js
Project Structure:

NOTE: 'uploads' is the folder where your files will be uploaded.
Example: This example demonstrates using formidable module to handle file uploads by parsing the request, saving the uploaded file to the uploads directory, and responding with "Successfully uploaded".
JavaScript
// Filename - index.js
const express = require('express');
const fs = require('fs');
const path = require('path')
const formidable = require('formidable');
const app = express();
app.post('/api/upload', (req, res, next) => {
const form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
let oldPath = files.profilePic.filepath;
let newPath = path.join(__dirname, 'uploads')
+ '/' + files.profilePic.name
let rawData = fs.readFileSync(oldPath)
fs.writeFile(newPath, rawData, function (err) {
if (err) console.log(err)
return res.send("Successfully uploaded")
})
})
});
app.listen(3000, function (err) {
if (err) console.log(err)
console.log('Server listening on Port 3000');
});
Steps to run the program:
Run the index.js file using the below command:
node index.js

Now open POSTMAN to run this API and send sample data as shown below:Â

Here in the body, we have passed send two fields, one is the name of type='Text' and the other is the profile pic of type='File' as shown above.
Now go to your uploads folder, your file is uploaded as shown below:
 
So this is how you can use a formidable module for uploading files and handling incoming form data easily and efficiently.
Similar Reads
How to handle file upload in Node.js ? File upload can easily be done by using Formidable. Formidable is a module that we can install on our project directory by typing the commandnpm install formidableApproach: We have to set up a server using the HTTPS module, make a form that is used to upload files, save the uploaded file into a temp
3 min read
How to upload files in firebase storage using ReactJS ? Firebase Storage is a powerful cloud storage solution provided by Google's Firebase platform. It allows developers to store and retrieve user-generated content, such as images, videos, and other files, in a secure and scalable manner. In this article, we will explore how to upload files to Firebase
2 min read
Upload Files to Local public folder in NodeJs using Multer Uploading files to a local public folder in Node.js using Multer involves a few steps. This article helps you set it up. Install Multer First, you need to install Multer, which is a Node.js middleware for handling multipart/form-data, primarily used for uploading files. npm install multerSet Up Your
3 min read
How to add authentication in file uploads using Node.js ? There are multiple ways to upload files and apply authentications to them. The easiest way to do so is to use a node module called multer. We can add authentication by restricting users on file uploads such as they can upload only pdf and the file size should be less than 1 Mb. There are many module
3 min read
How to Upload File with Progress Bar using Node.js ? Uploading files in NodeJS involves transferring files from the client's location to the server's location. A progress bar is a visual component that displays the percentage of the total file upload. In this tutorial, we will create a local server and demonstrate how to upload files to a destination
4 min read
How to test file uploads in Postman using Express? In web development, handling file uploads is a major requirement for various APIs. Postman is a powerful tool used for API testing, facilitates the process of testing file uploads. In this article we will see how we can test file upload with the help of Postman. Prerequisites:Node and NPM/Yarn insta
3 min read
How to Handle file upload in Node with Multer and Postman In this article, we will discuss about file upload functionality using Node, Multer and Postman. File Uploading is a significant functionality of any web service to get the data from the users in the form of files.To implement this feature we will use Node JS with Express and Multer library. Finally
7 min read
How to Upload Single/Multiple Image to Cloudinary using Node.js ? Uploading images to Cloudinary from a Node.js application is a common task when you need to manage and store images in the cloud. Cloudinary is a cloud-based service that provides comprehensive solutions for image and video management, including upload, storage, manipulation, and delivery.Approachto
4 min read
How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
4 min read
How to resolve a "Cannot find module" error using Node.js? This article outlines the steps you can take to troubleshoot and fix "Cannot find module" errors in your Node.js projects. These errors typically arise when Node.js cannot locate a module you're trying to use in your code. Table of Content Error "Cannot find module" Approach to Solve the ErrorInstal
3 min read