Node.js – Error: Cannot find module ‘pug’


Here is my index.js file:

const express = require('express')
const app = express()

app.set('views', __dirname + '/views');
app.set('view engine', 'pug')

app.get('/', function (req, res) {
  res.render('index', { title: 'Hey', message: 'Hello there!' })

app.listen(3333, function () {
  console.log('Example app listening on port 3333!')

index.pug file:

    title= title
    h1= Hello

package.json file:

  "name": "@npm-private/pug_with_node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.15.3",
    "jade": "^1.11.0",
    "pug": "^2.0.0-rc.2"

When I run my server file then it shows me an error. in fact, I install pug and jade both npm modules:

Error: Cannot find module 'pug'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at new View (/home/software/node_modules/express/lib/view.js:80:30)
at Function.render (/home/software/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/software/node_modules/express/lib/response.js:971:7)
at /home/software/Harsh Patel/pug_with_node/index.js:8:7
at Layer.handle [as handle_request] (/home/software/node_modules/express/lib/router/layer.js:95:5)
at next (/home/software/node_modules/express/lib/router/route.js:137:13)

Best Solution

Try to add this line

app.engine('pug', require('pug').__express)


app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

This solved the same problem for me!

Related Question