18
votes

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)

13
What command did you use to install those two modules? - Syntactic Fructose
Where is package.json? Did you do npm install - Arpit Solanki
make sure you have pug inside tour package.json - Ved
I have pug inside my package.json - Harsh Patel
I added package.json file as well - Harsh Patel

13 Answers

21
votes

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!

13
votes

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
5
votes

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

3
votes

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.

2
votes

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.

2
votes

Install

npm i pug

Put

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

before

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


1
votes

Run following Commands..

  1.npm remove pug express

  2.npm install pug express

This will solve the issue

0
votes

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"
  }
0
votes

Reinstalling pug fixed this for me:

yarn remove pug
yarn add pug

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

0
votes

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.

Please! accept the answer if it worked for you.

0
votes

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

-1
votes

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

-1
votes

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.