/errors/javascript/x-is-not-a-function
TypeError: x is not a function (JavaScript)
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v24.20.0updated
- tool
- JavaScript
- exit code
- 1
- versions
- ECMAScript (all), Node.js and browsers
- os
- any
TypeError: x is not a function means the code calls something with () that is not a function. The text before "is not a function" is the expression that was called, so user.getName is not a function says that user.getName is undefined (or a non-function value). Check what the value really is with console.log(typeof user.getName), then fix the name, the import or the type.
Reproduce it
cat > app.js <<'EOF'
const user = { name: "Ada" };
console.log(user.getName());
EOF
node app.js 2>&1 | grep TypeError
exit ${PIPESTATUS[0]}Output · exit 1
TypeError: user.getName is not a function
The usual suspects
cat > cases.js <<'EOF'
const tryCall = (label, f) => {
try { f(); } catch (e) { console.log(`${label.padEnd(22)} ${e.name}: ${e.message}`); }
};
const data = { items: [1, 2, 3] };
tryCall("object, not array", () => data.map((x) => x));
const price = "5";
tryCall("string, not number", () => price.toFixed(2));
const lib = { default: () => "ok" };
tryCall("wrong import shape", () => lib());
let format = (x) => x.trim();
format = "oops";
tryCall("overwritten variable", () => format(" a "));
console.log("fixed:", data.items.map((x) => x * 2), Number(price).toFixed(2), lib.default());
EOF
node cases.jsOutput
object, not array TypeError: data.map is not a function string, not number TypeError: price.toFixed is not a function wrong import shape TypeError: lib is not a function overwritten variable TypeError: format is not a function fixed: [ 2, 4, 6 ] 5.00 ok
Fixes
- Typo or wrong object:
console.log(Object.keys(obj))shows which methods exist. Methods defined on a class live on its prototype, soobj.methodis undefined whenobjis a plain JSON object rather than a class instance. - Array methods on non-arrays: API responses often wrap the array (
response.items,response.data). DOMquerySelectorAllreturns aNodeList, which hasforEachbut notmap: useArray.from(list).map(...). - Imports:
import fn from "./mod.js"reads the default export;import { fn } from "./mod.js"reads a named one. With CommonJS interop, the function may be on.default. - Optional callbacks:
options.onDone?.()calls the function only if it exists.
Sources
- developer.mozilla.orgdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function
- developer.mozilla.orgdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
- developer.mozilla.orgdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
Related pages
errors · javascriptTypeError: Cannot read properties of undefined (reading 'x')JavaScript throws this TypeError when it reads a property of a value that is undefined. Find which object is missing, then guard it with ?. or a default.glossary · javascriptWhat is hoisting in JavaScript?Hoisting: JavaScript creates declarations before a scope runs. var is undefined until assigned, functions work early, and let and const throw in the TDZ.errors · nodejsError: Cannot find module '…' in Node.jsNode.js throws Cannot find module when require() or import cannot resolve a path or package. Install the package, fix the relative path or add the extension.commands · javascriptjavascript remove duplicates from arrayRemove duplicates from a JavaScript array with [...new Set(arr)], keep first-seen order, handle NaN, and deduplicate arrays of objects by a key with a Map.