Fix "TypeError: x is not a function" in JavaScript
Uncaught TypeError: data.map is not a function
- TypeError: data.map is not a function. (In 'data.map((item) => item.id)', 'data.map' is undefined)
- TypeError: this.setState is not a function
- TypeError: (0 , _utils.formatDate) is not a function
- TypeError: Object(...) is not a function
- TypeError: $(...).modal is not a function
You are calling with () a value that is not a function: here data.map does not exist because data is not an array (often an object that wraps the array, an unparsed JSON string or undefined). Print the real type of the value, then fix its source: the right response field, the right import, the right method name.
What this error means
To run a.b(), the engine first reads a.b, then checks that the result is callable. If it is undefined, a string, a number or a plain object, it throws a TypeError. Unlike "Cannot read properties of undefined", the object before the dot does exist: it is the property that is missing or not a function. Chrome, Edge, Node.js and Firefox print "x is not a function", Safari adds "(In '…', '…' is undefined)". With a bundler (webpack, Vite, Babel), an import that resolves to undefined shows up as "(0 , _module.fn) is not a function" or "Object(...) is not a function".
Common causes
- Wrong data type:
.map(),.filter()or.forEach()on an object ({ items: [...] }), on a JSON string never passed toJSON.parse(), or on anHTMLCollectionthat does not have those methods. - Default import instead of a named import (or the reverse):
import formatDate from './utils'while the module doesexport function formatDate, or arequire()that returns{ default: fn }. - Missing or misspelled method:
getElementByIDinstead ofgetElementById,array.contains()instead ofincludes(), or a method from a newer version of the library. - Callback prop not passed (React, Vue): the component calls
props.onSave()but the parent never passedonSaveto it. - jQuery plugin missing or jQuery loaded twice:
$(...).modal is not a functionwhen the plugin loads before jQuery, or a second copy of jQuery overwrites the first one and its plugins.
Step-by-step fix
-
Print the real type of the value
Right before the line shown in the stack trace, log the value and its type.
Array.isArray()tells you immediately whether you have an array.console.log(typeof data, Array.isArray(data), data); console.log(typeof data?.map); -
Get the actual array
If the API returns an object, take the right field; if it is a string, parse it; for an object or a DOM list, convert to an array before calling
.map().const res = await fetch('/api/users'); const body = await res.json(); const users = Array.isArray(body) ? body : body.data ?? []; const values = Object.values({ a: 1, b: 2 }).map((n) => n * 2); const texts = Array.from(document.getElementsByClassName('item')).map((el) => el.textContent); -
Check imports and exports
An
export functionis imported with braces, anexport defaultwithout. In CommonJS, an ESM package loaded withrequire()often exposes its function under.default.// utils.js export function formatDate(date) { return new Intl.DateTimeFormat('en-GB').format(date); } // app.js import { formatDate } from './utils.js'; // not: import formatDate from './utils.js' // CommonJS const { formatDate: format } = require('./utils.cjs'); -
Make callback props safe
Give optional functions a default value or call them with
?.(), and check that the parent passes the function itself.function SearchBox({ onSearch = () => {} }) { return <input onChange={(e) => onSearch(e.target.value)} />; } // optional callback props.onClose?.(); -
Load jQuery once, before its plugins
Look for jQuery
<script>tags in the page source: there must be only one, placed before the plugins. With Bootstrap 5, jQuery is no longer required: use its native JavaScript API.// Bootstrap 5, no jQuery const modal = bootstrap.Modal.getOrCreateInstance(document.querySelector('#myModal')); modal.show();
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Use TypeScript in
strictmode: calling.map()on a type that is not an array is flagged at compile time. - Validate the shape of API responses at the boundary (for example with Zod) instead of assuming they return an array.
- Enable the ESLint rule
import/named(eslint-plugin-import) to catch named imports that do not exist in the module.
Frequently asked questions
Why ".map is not a function" when my API does return data?
The data is probably an object that wraps the array ({ data: [...] }), or the response was not parsed. With axios, the body is in res.data; with fetch, you need await res.json().
What does "(0 , _utils.default) is not a function" mean?
It is bundler-generated code for a default import. The module has no default export: use a named import { fn } or add export default.
Why forEach is not a function with getElementsByClassName?
It returns an HTMLCollection, which has no forEach. Use querySelectorAll() (a NodeList, which has forEach) or Array.from() to get a real array.
Useful tools for this error
Error finder
Paste an error message or a log: the tool recognises the error and opens the right fix.
JSON validator
Validate, format and minify JSON, with the exact line and column of the error.
Stack trace formatter
Formats a stack trace (JS, Python, PHP, Java, .NET, Go), separates your code from libraries and points to the line to open first.
More JavaScript & Node.js errors
- Fix "Cannot read properties of undefined" in JavaScript
- Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"
- Fix "Unexpected token '<', "<!DOCTYPE "... is not valid JSON"
- Fix "Error: Cannot find module" in Node.js
- Fix "npm ERR! ERESOLVE unable to resolve dependency tree"
- Fix "Hydration failed because the server rendered HTML didn’t match the client" (React, Next.js)
- Fix "Too many re-renders. React limits the number of renders" in React
- Fix "Error: listen EADDRINUSE: address already in use" in Node.js