Skip to content
  1. LuzineDebug
  2. JavaScript & Node.js
  3. Uncaught TypeError: data.map is not a function
JavaScript & Node.js Beginner ≈ 10 min to fix

Fix "TypeError: x is not a function" in JavaScript

Error message
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
In short

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.

Be the first to confirm this fix

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

  1. Wrong data type: .map(), .filter() or .forEach() on an object ({ items: [...] }), on a JSON string never passed to JSON.parse(), or on an HTMLCollection that does not have those methods.
  2. Default import instead of a named import (or the reverse): import formatDate from './utils' while the module does export function formatDate, or a require() that returns { default: fn }.
  3. Missing or misspelled method: getElementByID instead of getElementById, array.contains() instead of includes(), or a method from a newer version of the library.
  4. Callback prop not passed (React, Vue): the component calls props.onSave() but the parent never passed onSave to it.
  5. jQuery plugin missing or jQuery loaded twice: $(...).modal is not a function when the plugin loads before jQuery, or a second copy of jQuery overwrites the first one and its plugins.

Step-by-step fix

  1. 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.

    javascript
    console.log(typeof data, Array.isArray(data), data);
    console.log(typeof data?.map);
  2. 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().

    javascript
    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);
  3. Check imports and exports

    An export function is imported with braces, an export default without. In CommonJS, an ESM package loaded with require() often exposes its function under .default.

    javascript
    // 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');
  4. Make callback props safe

    Give optional functions a default value or call them with ?.(), and check that the parent passes the function itself.

    javascript
    function SearchBox({ onSearch = () => {} }) {
      return <input onChange={(e) => onSearch(e.target.value)} />;
    }
    
    // optional callback
    props.onClose?.();
  5. 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.

    javascript
    // 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 strict mode: 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

More JavaScript & Node.js errors

All JavaScript & Node.js errors →