22

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:

html
  head
    title= title
  body
    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)

Harsh Patel
  • 6,334
  • 10
  • 40
  • 73

15 Answers15

24

Try to add this line

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

before

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

This solved the same problem for me!

Sergi Nadal
  • 900
  • 13
  • 23
  • No I can't explain! I did more projects with express & pug, and I didn't have that error anymore. In fact, I didn't use the line app.engine('pug', require('pug').__express) anymore. – Sergi Nadal May 14 '18 at 06:26
  • 2
    All I needed to do was add `"pug": "^2.0.3` under `dependencies` in `package.json`... – Ronnie Royston Oct 01 '18 at 20:14
  • Life saver! For me this started to occur when compiling the server with webpack (I did `__non_webpack_require__("pug").__express` to preserve Node require – Dominic Jul 10 '23 at 21:58
19

When there is a mismatch of module installation between Global and Local you will encounter this issue even if you have installed it all the modules. I would suggest you to install everything local to the project by including the dependency in the package.json

npm install --save express jade pug
Gopesh Sharma
  • 6,730
  • 4
  • 25
  • 35
5

The simplest fix is to install pug as a development dependency: npm i -D pug

deXtro
  • 51
  • 1
  • 1
5

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

before

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

works for me.

After I tried different methods listed. My understanding based on the official document, express by default uses app.engine() function where the callback function need to follow .__express syntax for 'pug' template specificlly.

Chloe
  • 83
  • 1
  • 6
3

Runnig: npm install express worked for me

I had been forgotten to install express locally.

Also make sure you installed pug. (Run: npm i pug)


More explaination:

In my system express works even if i don't install it locally (without npm install express). so express couldn't find local pug module, because it was running from somewhere else.

Note that if you have express in your dependencies, it doesn't mean that you installed it. run npm install to make sure all of dependencies are installed.

yaya
  • 7,675
  • 1
  • 39
  • 38
2

Run following Commands..

  1.npm remove pug express

  2.npm install pug express

This will solve the issue

Jaimil Patel
  • 1,301
  • 6
  • 13
iohwio
  • 119
  • 1
  • 1
2

Install

npm i pug

Put

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

before

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


  • This worked @recherche-shafeeq but please can you explain why it is needed for it to run smoothly? I checked other resources online but their explanations were not clear – Ekanem Eno Aug 24 '20 at 04:25
  • @Ekanem Eno because in order to run pug code you need to first install pug engine just like nodejs is installed as a run time environment for javascript.Please refer to pug docs to understand what pug is https://pugjs.org/api/getting-started.html – recherche-shafeeq Aug 29 '20 at 13:24
0

in the terminal in your project install the pug like that:

npm install --save ejs pug express-handlebars

in app.js express

const app = express();

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

in the package.json should look like this

  "dependencies": {
    "body-parser": "^1.18.3",
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "express-handlebars": "^3.0.0",
    "pug": "^2.0.3"
  }
Faris
  • 1,206
  • 1
  • 12
  • 18
0

Reinstalling pug fixed this for me:

yarn remove pug
yarn add pug

Thanks to Ron Royston for the hint: Error: Cannot find module 'pug'

David Lemayian
  • 2,679
  • 1
  • 20
  • 18
0

It is very simple if you are doing it for Nodejs express framework. You can follow any of the below options

  1. If you have installed pug globally like adding -g then install pug once again in your project as local npm install pug

  2. if the first option is still not working for you then add the following line in your package.json just after "express": "^4.17.1" in the dependency object.

"pug": "^3.0.0"

For me, the first method worked because if you follow the first method then the second will be automatically done.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Anup Kumar
  • 61
  • 3
0

See in your package.json that your express and pug dependencies was installed or not If any of them is not installed then installed them by just using

npm i express

npm i pug 

And your problem will remove

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

first verify the package express and pug and if u are open multi-project on your laptop make sure the the port that u use is not used because I got this bug ad when I change the port number is fix fixed. const path = require('path') // you don't need to install it app.set('the folder that contain the pug file',path.join(__dirname,'the folder that contain the pug file'); app.set('view engine','pug')

0

Make sure both (express and pug) are listed as dependencies in your package.json

"dependencies": {
    "esm": "^3.2.25",
    "express": "^4.18.1",
    "mysql2": "^2.3.3",
    "pug": "^3.0.2",
    "sequelize": "^6.21.2"
},
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
almentero
  • 1
  • 2
-1

I had this issue while doing nodeschool.io workshop. I looked where the workshop's compiler was looking for the module and when I manually checked /users/@yourUser/node_modules/ <-(UNIX Mac environment) it was missing. Installing Pug locally fixed the issue with npm install pug. In recent versions of Node is not necessary to add the --save flag. If you want to have the pug module added to your node-modules path just ad the -g flag after your install, example: npm install pug -g -g stands for global

Franko
  • 1
-1

Many times, even after doing everything right, the error still occurs just because of a tiny mistake of adding a space after 'pug' i.e.,

app.set('view engine','pug ')

Such a thing can easily get overlooked while checking your code. So do this instead.

app.set('view engine','pug')

Since I have just started learning about express and pug, I faced this issue and realized my problem.