できない.dev

Express 5「req.param is not a function」の原因と直し方

Express 5 の TypeError: req.param is not a function は、5 で消えた req.param() を呼ぶと出る。
値の出どころに合わせて req.params・req.body・req.query から読めば直る。

公開: 更新:

症状できない · exit 22
curl -s --fail-with-body http://localhost:3000/users/7
{"error":"TypeError: req.param is not a function"}
解決後できた · exit 0
curl -s --fail-with-body http://localhost:3000/users/7
{"id":"7"}

2026-11-07 に node:24(Express 5.3.0 / Node.js 24.22.2) で実際に打って取った出力。検証環境の詳細

要約

Express 5 に上げると、req.param() を呼ぶルートだけが 500 になり、サーバーのログに次の例外が出る。

TypeError: req.param is not a function

サーバーは起動でき、ほかのルートも動く。
req.param(name) は、パスのパラメータ・ボディ・クエリの順に名前を探して値を返すメソッドだった。
4 で非推奨になり、5 で削除された(移行ガイド(新しいタブで開く)の「req.param(name)」)。

4 で新しい名前に置き換わった res.sendfile() や req.acceptsLanguage() も 5 で消え、呼ぶと同じ形の is not a function になる。

実行例

node:24 のコンテナで Express 5.3.0 を入れ、4 の書き方のままのルートを curl で叩くと、3 つとも TypeError の 500 で終了コード 22 になった(--fail-with-body は 400 以上の応答で本文を出す)。
app.del() を書いたファイルは、起動の時点で終了コード 1 で落ちた。

$ npm ls express
tmp.h4i2v0qedi@1.0.0 /tmp/tmp.h4I2v0qEDi
`-- express@5.3.0
$ cat app.js
const express = require("express");
const app = express();
 
app.get("/users/:id", (req, res) => res.json({ id: req.param("id") }));
app.get("/report", (req, res) => res.sendfile("report.txt"));
app.get("/lang", (req, res) => res.json({ ja: req.acceptsLanguage("ja") }));
 
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ error: String(err) });
});
 
app.listen(3000, () => console.log("listening on 3000"));
$ node app.js > server.log 2>&1 &
$ curl -s --fail-with-body http://localhost:3000/users/7
{"error":"TypeError: req.param is not a function"}
$ echo $?
22
$ curl -s --fail-with-body http://localhost:3000/report
{"error":"TypeError: res.sendfile is not a function"}
$ echo $?
22
$ curl -s --fail-with-body -H "Accept-Language: ja" http://localhost:3000/lang
{"error":"TypeError: req.acceptsLanguage is not a function"}
$ echo $?
22
$ head -n 3 server.log
listening on 3000
TypeError: req.param is not a function
    at /tmp/tmp.h4I2v0qEDi/app.js:4:56
$ cat app.js
const express = require("express");
const app = express();
 
app.get("/users/:id", (req, res) => res.json({ id: req.params.id }));
app.get("/report", (req, res) => res.sendFile("report.txt"));
app.get("/lang", (req, res) => res.json({ ja: req.acceptsLanguages("ja") }));
 
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ error: String(err) });
});
 
app.listen(3000, () => console.log("listening on 3000"));
$ node app.js > server.log 2>&1 &
$ curl -s --fail-with-body http://localhost:3000/users/7
{"id":"7"}
$ echo $?
0
$ curl -s --fail-with-body http://localhost:3000/report
{"error":"TypeError: path must be absolute or specify root to res.sendFile"}
$ echo $?
22
$ curl -s --fail-with-body -H "Accept-Language: ja" http://localhost:3000/lang
{"ja":"ja"}
$ echo $?
0
$ grep -n sendFile app.js
5:app.get("/report", (req, res) => res.sendFile("report.txt", { root: __dirname }));
$ node app.js > server.log 2>&1 &
$ curl -s --fail-with-body http://localhost:3000/report
2026-11 report
$ echo $?
0
$ cat admin.js
const express = require("express");
const app = express();
 
app.del("/users/:id", (req, res) => res.sendStatus(204));
 
app.listen(3000, () => console.log("listening on 3000"));
$ node admin.js > admin.log 2>&1
$ echo $?
1
$ head -n 5 admin.log
/tmp/tmp.h4I2v0qEDi/admin.js:4
app.del("/users/:id", (req, res) => res.sendStatus(204));
    ^
 
TypeError: app.del is not a function
$ grep -n delete admin.js
4:app.delete("/users/:id", (req, res) => res.sendStatus(204));
$ node admin.js > server.log 2>&1 &
$ curl -s --fail-with-body -X DELETE -w '%{http_code}\n' http://localhost:3000/users/7
204
$ echo $?
0

— 2026-11-07 時点の出力

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 2 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • Express 5.3.0

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. 値の出どころに合わせて読む

app.get("/users/:id", (req, res) => res.json({ id: req.params.id }));

req.param("id") を書き換えるときは、値の出どころを確かめる。
:id のようなパスの値は req.params、フォームや JSON の値は req.body、?id=7 の値は req.query から読む。
移行ガイドには、req.param() を書き換える codemod(@expressjs/explicit-request-params)も載っている。

2. 新しい名前に書き換える

res.sendfile() を res.sendFile() にするときは、パスの渡し方も変える。
4 の res.sendfile("report.txt") は、node を起動したディレクトリからファイルを探した。
res.sendFile() は絶対パスか root の指定を求め(Response の API(新しいタブで開く))、名前だけを変えると TypeError: path must be absolute or specify root to res.sendFile になる。

app.get("/report", (req, res) => res.sendFile("report.txt", { root: __dirname }));
app.get("/lang", (req, res) => res.json({ ja: req.acceptsLanguages("ja") }));

root に __dirname を渡すと、どのディレクトリから起動しても app.js と同じディレクトリから探す。

3. app.del() を app.delete() にする

app.delete("/users/:id", (req, res) => res.sendStatus(204));

上げる前に 4.22.3 のまま一通りリクエストを流すと、ここに挙げたメソッドを呼んだ箇所ごとに express deprecated の警告が出て、末尾に app.js:4:56 のような位置が付く。

よくある原因

  1. req.param("id") を呼んでいる。
    4.22.3 では呼んだ箇所ごとに express deprecated req.param(name) の警告がログに出るだけで、値は返っていた。
  2. 4 で名前が変わったメソッドを、古い名前で呼んでいる。
    5 で消えたのは res.sendfile()・req.acceptsLanguage()・req.acceptsCharset()・req.acceptsEncoding() の 4 つで、新しい名前は res.sendFile()・req.acceptsLanguages()・req.acceptsCharsets()・req.acceptsEncodings() である。
  3. app.del() でルートを登録している。
    ルートの登録はファイルを読み込んだ時点で走るので、TypeError: app.del is not a function でサーバーが起動できない。