- JavaScript
is not a functionの原因と直し方|JavaScript
TypeError: res.map is not a function は、関数ではない値を、関数として呼び出したときのエラーです。 エラー文の res.map の部分が、呼び出そうとした式です。その式の値が、関数ではなかった(undefined・オブジェクト・文字列など)ということです。
まず、その式の左側の値が何かを確かめます。
console.log(res, typeof res, Array.isArray(res)); // res.map なら res を確かめる
| エラー文の例 | 原因 | 直し方 |
|---|---|---|
res.map is not a function |
res が配列ではない(オブジェクト・undefined) |
配列の場所(res.items など)を確かめる |
document.querySelectorAll(...).map is not a function |
NodeList は配列ではない | Array.from(...) にしてから map |
document.getElementByID is not a function |
メソッド名の打ち間違い(正しくは getElementById) |
綴りを直す |
p.json is not a function |
await し忘れ(Promise に対して呼んだ) | await を付ける |
total is not a function |
関数と同じ名前の変数で上書きした | 名前を変える |
1 is not a function |
セミコロンなしで、次の行の ( とつながった |
行末にセミコロン |
formatDate is not a function |
require・import で受け取り方を間違えた |
{ } で取り出す |
確認環境:Node.js 22.22.2、Chromium 141(確認日 2026年10月11日)。以下のエラーはすべて実際に実行して確かめました。
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「is not a functionの原因と直し方|JavaScript」
原因1:配列ではない値に map・forEach
const res = { items: [1, 2, 3] };
res.map(x => x * 2); // TypeError: res.map is not a function
res.items.map(x => x * 2); // [2, 4, 6]
const users = { a: { name: "佐藤" }, b: { name: "鈴木" } };
users.forEach(u => u); // TypeError: users.forEach is not a function
Object.values(users).map(u => u.name); // ["佐藤", "鈴木"]
API の応答が { items: [...] } のように、配列を包んだオブジェクトになっていることがよくあります。console.log で形を確かめ、配列のある場所で map します。オブジェクトをループしたいなら Object.values・Object.entries で配列にします。
NodeList・HTMLCollection(ブラウザ)
document.querySelectorAll("li").map(li => li.textContent);
// TypeError: document.querySelectorAll(...).map is not a function
Array.from(document.querySelectorAll("li")).map(li => li.textContent); // ["a", "b"]
document.querySelectorAll("li").forEach(li => ...); // forEach は使える
document.getElementsByClassName("a").forEach(...);
// TypeError: document.getElementsByClassName(...).forEach is not a function
| 取得のしかた | forEach | map・filter |
|---|---|---|
querySelectorAll |
使える | 使えない |
getElementsByClassName・getElementsByTagName |
使えない | 使えない |
Array.from(...)・[...] にしたもの |
使える | 使える |
見た目は配列でも、DOM の取得結果は配列ではありません。 map や filter を使うなら Array.from で配列にします。
原因2:名前の打ち間違い・存在しないメソッド
document.getElementByID("x"); // TypeError: document.getElementByID is not a function(正しくは Id)
const s = "a,b"; s.push("c"); // TypeError: s.push is not a function(文字列に push は無い)
(123).toUpperCase(); // TypeError: 123.toUpperCase is not a function
const cfg = {}; cfg.onLoad(); // TypeError: cfg.onLoad is not a function(未設定のコールバック)
呼び出したメソッドが、その値の型に存在するかを確かめます。コールバックのように「設定されていないことがある関数」は、呼ぶ前に確かめます。
cfg.onLoad?.(); // onLoad が無ければ何もしない
if (typeof cfg.onLoad === "function") cfg.onLoad();
原因3:await し忘れ
const res = fetch("/api/users");
const data = res.json(); // TypeError: res.json is not a function
fetch は Promise を返すので、await を付けないと res は応答ではなく Promise です。
const res = await fetch("/api/users");
const data = await res.json();
.then is not a function は逆に、Promise ではない値に .then を付けたときに出ます。
原因4:変数で関数を上書きした
let total = (a) => a.reduce((s, x) => s + x, 0);
total = total([1, 2]); // total に数値の 3 が入る
total([3]); // TypeError: total is not a function
1回目は動き、2回目から落ちるのが特徴です。ループの中や、同じ名前の変数・引数で関数を隠していないかを確かめます。関数は const で宣言すると、再代入がエラーになるので気づけます。
原因5:セミコロンなしで、次の行とつながる
const a = 1
(function () { console.log("x") })()
// TypeError: 1 is not a function
行の先頭が ( だと、JavaScript は前の行の続きとして読み、1(function () {...}) と解釈しました。セミコロンを付けない書き方をしているなら、(・[・` で始まる行の前にセミコロンを置きます。
const a = 1
;(function () { console.log("x") })()
原因6:require・import の受け取り方の間違い
// util.cjs
module.exports = { formatDate: (d) => d.toISOString() };
| 受け取り方 | 結果 |
|---|---|
const formatDate = require("./util.cjs"); formatDate(...) |
TypeError: formatDate is not a function(中身はオブジェクト) |
const { formatDate } = require("./util.cjs") |
動く |
import formatDate from "./util.cjs"; formatDate(...) |
TypeError: formatDate is not a function |
import pkg from "./util.cjs"; const { formatDate } = pkg; |
動く |
import { formatDate } from "./util.cjs" |
SyntaxError: Named export 'formatDate' not found.(下で説明) |
module.exports = { ... } で書き出したものを、そのまま受け取ると、関数ではなくオブジェクトです。{ } で取り出します。
import { } で受け取れない CommonJS もある
SyntaxError: Named export 'formatDate' not found. The requested module './util.cjs' is a CommonJS module,
which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from './util.cjs';
const { formatDate } = pkg;
確認環境の Node.js 22 では、module.exports = { formatDate: (d) => ... } の形の CommonJS から import { formatDate } で受け取れず、エラー文の案内どおり default で受け取ってから取り出すと動きました。exports.formatDate = ... の形で書き出したファイルなら、import { formatDate } で受け取れました。
ESM どうしの default と名前付き
// util.mjs
export function formatDate(d) { ... } // 名前付きで書き出し
// use.mjs
import formatDate from "./util.mjs";
// SyntaxError: The requested module './util.mjs' does not provide an export named 'default'
ESM どうしでは、is not a function ではなく、読み込みの時点で SyntaxError になりました。書き出し側が export default か export function かに合わせて、import x from と import { x } from を使い分けます。
確認の手順(チェックリスト)
| 順番 | 確認すること | 方法 |
|---|---|---|
| 1 | 呼び出した式 | エラー文の xxx.yyy is not a function の xxx |
| 2 | その値の中身と型 | console.log(xxx, typeof xxx, Array.isArray(xxx)) |
| 3 | 配列か | オブジェクトなら配列の場所、NodeList なら Array.from |
| 4 | 綴り・存在するメソッドか | getElementById など大文字小文字も |
| 5 | Promise ではないか | await |
| 6 | 上書き・セミコロン | 同じ名前の変数、( で始まる行 |
| 7 | require・import | { } で取り出すか、default で受け取るか |
よくある質問
Q. 「undefined is not a function」と出ます。
A. 呼び出した値が undefined だったという意味で、原因は同じです。どの式が undefined かは、エラーの行番号と、その行で呼び出している関数を console.log して確かめます。
Q. jQuery で「$ is not a function」と出ます。
A. $ に jQuery 以外のものが入っているか、jQuery が読み込まれる前に使っています。読み込みの順番と、$ を別のライブラリが使っていないかを確かめます。
まとめ
- 関数ではない値を関数として呼んだエラー。エラー文の左側の値を
console.logで確かめる - いちばん多いのは配列ではない値への map・forEach(API の応答・NodeList)
- 打ち間違い・await し忘れ・変数での上書きも同じエラーになる
- セミコロンなしで
(から始まる行は前の行とつながる(1 is not a function) module.exports = { }は{ }で取り出す。CommonJS からimport { }できないときは default で受け取る
参考・出典
確認日はいずれも 2026年10月11日です。
- MDN Web Docs「TypeError: "x" is not a function」:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「is not a functionの原因と直し方|JavaScript」
株式会社RJC ― SI事業・SES事業・AI駆動開発。RJCは一緒に成長を楽しめる会社です。
WE ARE HIRING
RJCで一緒に開発しながら、
成長を楽しみませんか?
RJCは、Web・モバイル・AIを活用した開発プロジェクトで、テックリードやPM・PMOも活躍するシステム開発会社です。会社を知る、待遇を確かめる、話を聞いてみる。気になるところ見てみてください!
- 127日年間休日
- 12時間平均残業時間
- 毎日ガチャ遊びココロも大切にする福利厚生。アマギフなどの賞品ラインナップ!
ほかにも、チケットレストラン、書籍読み放題、2年ごとの慰労報奨(休暇 or 金一封)、11期連続の黒字決算。