This error occurs when Express cannot locate the view engine you specified in your application. It typically happens when the view engine package is not installed, the engine name is misspelled, or the engine is not properly registered.
The "View engine not found" error means Express tried to render a view using a template engine (like Pug, EJS, Handlebars, etc.) but couldn't find that engine installed in your project. Express relies on view engines to transform template files into HTML that gets sent to the browser. When you set a view engine with `app.set('view engine', 'pug')` and then render a view with `res.render('myTemplate')`, Express looks for an installed npm package that provides that engine. If the package isn't installed or isn't registered correctly, you get this error. The error message includes the name of the missing engine, making it easy to identify which package you need to install.
Install the missing view engine package locally in your project. For Pug:
npm install pugFor other popular engines:
npm install ejs
npm install hbs
npm install handlebars
npm install nunjucks
npm install mustacheAlways use npm install (not -g global flag) to install packages that your application code depends on.
Ensure the engine name in your Express code exactly matches the installed package. For example:
const express = require('express');
const app = express();
// Set the view engine to pug
app.set('view engine', 'pug');
// Set the views directory (where template files are stored)
app.set('views', './views');
app.get('/', (req, res) => {
// This will look for ./views/index.pug
res.render('index', { title: 'Home' });
});Common engine names:
- pug - for the pug package
- ejs - for the ejs package
- hbs - for the hbs (Handlebars) package
- nunjucks - for the nunjucks package
Avoid typos and use lowercase (engine names are case-sensitive).
Open your package.json and verify the view engine is listed in dependencies (not devDependencies):
{
"dependencies": {
"express": "^4.18.2",
"pug": "^3.0.2"
}
}If the package is only in devDependencies, move it to dependencies. Then check that node_modules exists and contains the package:
ls node_modules/pugIf node_modules is missing or incomplete, run a clean reinstall:
rm -rf node_modules package-lock.json
npm installEnsure the directory specified with app.set('views', path) exists and contains your template files:
const path = require('path');
const express = require('express');
const app = express();
// Correct: Use path.join for cross-platform compatibility
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.get('/', (req, res) => {
// Express will look for ./views/index.pug
res.render('index');
});Create the views directory if it doesn't exist:
mkdir -p viewsPlace your template files in this directory with the appropriate extension (e.g., index.pug, profile.pug).
If you're using a less common view engine or a custom implementation, you may need to explicitly register it with Express:
const express = require('express');
const nunjucks = require('nunjucks');
const app = express();
// Register nunjucks as the view engine
nunjucks.configure('views', { autoescape: true });
app.engine('html', nunjucks.render);
app.set('view engine', 'html');
app.get('/', (req, res) => {
res.render('index');
});Check the documentation of your specific view engine for registration instructions, especially for less common engines or custom solutions.
Production deployments: View engines must be in dependencies, not devDependencies. When deploying to platforms like Heroku, Railway, or Vercel, npm install --production skips devDependencies, causing this error if your engine is misplaced.
Multiple view engines: You can register multiple view engines in the same Express app:
app.engine('pug', require('pug').__express);
app.engine('html', require('nunjucks').render);Docker/containerization: When using Docker, ensure your view engine is installed during the build phase. Your Dockerfile should copy package.json and run npm install before copying your application code.
TypeScript with Express: When using TypeScript with Express, ensure @types/express is installed and the view engine's types are available (some engines include TypeScript definitions, others require separate @types packages).
Custom view engines: If you're building a custom view engine, ensure it's properly exported and accessible. Custom engines should follow Express conventions for engine functions.
Error: EMFILE: too many open files, watch
EMFILE: fs.watch() limit exceeded
Error: Middleware next() called multiple times (next() invoked twice)
Express middleware next() called multiple times
Error: Worker failed to initialize (worker startup error)
Worker failed to initialize in Node.js
Error: EMFILE: too many open files, open 'file.txt'
EMFILE: too many open files
Error: cluster.fork() failed (cannot create child process)
cluster.fork() failed - Cannot create child process