1
1

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 1 year has passed since last update.

Error: Failed to lookup view "index" in views directory "/Users/***/views"

Last updated at Posted at 2022-10-18

Error: Failed to lookup view "index" in views directory "/Users/***/views"

Node.js + Express でテンプレートエンジンをejsで設定するとエラーとなった

フォルダ構成

エラー時のappフォルダ以下の構成

├── package.json
├── src
│   ├── index.ts
│   ├── routes
│   │   └── index.js
│   └── views
│       └── index.ejs
├── tsconfig.json
├── yarn-error.log
└── yarn.lock

解決した方法

app.set('views', __dirname + '/views')を追加

index.ts
import express from 'express';

const app: express.Express = express();
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views'); // ここを追加
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Listener
app.listen(3000, () => {
  console.log('Start on ', app.listen().address());
});

app.get('/', (req, res) => {
  res.render('index');
});

Reference

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?