20
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「TypeError: Router.use() requires a middleware function but got a Object」というエラーが出たときの対応

Last updated at Posted at 2019-06-12

Nonde.js express でサイトを実装中に出たエラーに対する備忘録。

エラー内容

TypeError: Router.use() requires a middleware function but got a Object

server    | [nodemon] app crashed - waiting for file changes before starting...
server    | [nodemon] restarting due to changes...
server    | [nodemon] starting `node src/index.js`
server    | /app/node_modules/express/lib/router/index.js:458
server    |       throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
server    |       ^
server    | 
server    | TypeError: Router.use() requires a middleware function but got a Object
server    |     at Function.use (/app/node_modules/express/lib/router/index.js:458:13)
server    |     at Function.<anonymous> (/app/node_modules/express/lib/application.js:220:21)
server    |     at Array.forEach (<anonymous>)
server    |     at Function.use (/app/node_modules/express/lib/application.js:217:7)
server    |     at Object.<anonymous> (/app/src/index.js:16:5)
server    |     at Module._compile (internal/modules/cjs/loader.js:774:30)
server    |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
server    |     at Module.load (internal/modules/cjs/loader.js:641:32)
server    |     at Function.Module._load (internal/modules/cjs/loader.js:556:12)
server    |     at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
server    |     at internal/main/run_main_module.js:17:11

解決方法

app/server/src/routes 内に新規作成したlogin.jsファイルの文末でmoduleをexportしていないことが原因でエラーが生じていたようです。

login.js
module.exports = router;

これで通りました!!
server側にjsファイルを作成した際は注意してみてください。

20
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?