FormData is not defined Error in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# FormData is not defined Error in JavaScript [Solved]

The "FormData is not defined Error" error occurs when we try to use the FormData() constructor on the server side, most commonly in a Node.js application.

To solve the error, install and import the form-data npm package.

formdata is not defined

If you got the error in the browser, make sure you haven't misspelled the FormData keyword (it's case-sensitive).

To install the form-data package, navigate to the root directory of your project (where your package.json file is) and run the following command:

shell
# ๐Ÿ‘‡๏ธ if you use NPM npm install form-data # ๐Ÿ‘‡๏ธ if you use YARN yarn add form-data

install form data package

If your project doesn't have a package.json file initialized, create one using the npm init -y command.
shell
npm init -y

issue npm init y command

Now we can import and use the FormData constructor in our Node.js code. Here's an example.

index.js
import FormData from 'form-data'; import fs from 'fs'; const form = new FormData(); form.append('field', 'my value'); form.append('file', fs.createReadStream('/pictures/avatar.png')); console.log(form);

import and use formdata constructor

The code for this article is available on GitHub

We imported the FormData constructor, initialized it, and added 2 fields to it - one that contains a string and one that contains a file stream.

A quick test shows that everything works as expected.

formdata result

We used the ES6 module import/export syntax to import the package. If you use an older version of Node.js, you have to use the require syntax.

index.js
const FormData = require('form-data'); const fs = require('fs'); const form = new FormData(); form.append('field', 'my value'); form.append('file', fs.createReadStream('/pictures/avatar.png')); console.log(form);

The code sample above achieves the same result, however, instead of using ES6 module syntax we used the older require syntax.

You can check out more examples of using the package in the NPM page of form-data.

If you want to avoid installing a third-party module, use the URLSearchParams class in Node.js.

index.js
import fs from 'fs'; const form = new URLSearchParams(); form.append('field', 'my value'); form.append('file', fs.createReadStream('/pictures/avatar.png'));
The code for this article is available on GitHub

The URLSearchParams API provides read and write access to the query of a URL.

If you use the axios module, you can send a POST request with form data to a server in the following way.

index.js
import axios from 'axios'; const form = new URLSearchParams(); form.append('abc', 'xyz'); form.append('qwe', 'asd'); async function makeRequest() { try { const res = await axios.get('https://example.com', { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: form, }); const data = res.data; console.log(data); } catch (err) { console.log(err); } } makeRequest();
The code for this article is available on GitHub

If you don't have axios installed, make sure to install it first.

shell
# ๐Ÿ‘‡๏ธ if you use NPM npm install axios # ๐Ÿ‘‡๏ธ if you use YARN yarn add axios

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev