Error: Cannot find module 'pug'
Asked Answered
G

15

24

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)

Geometrid answered 27/7, 2017 at 5:47 Comment(11)
What command did you use to install those two modules?Incidental
Where is package.json? Did you do npm installKaciekacy
make sure you have pug inside tour package.jsonArianearianie
I have pug inside my package.jsonGeometrid
I added package.json file as wellGeometrid
I used: 1) npm install pug@latest -S (2) npm install jade@latest -SGeometrid
@HarshPatel You need to have jade, pug and express all in package.jsonTiannatiara
@GopeshSharma Yes I have all in package.json fileGeometrid
@HarshPatel Check you node modules and confirm pug is there.Arianearianie
@Arianearianie I checked twice!Geometrid
I delete my package.json file & node_modules directory and re install all packages. Then I got correct solution.Geometrid
T
26

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!

Tyler answered 27/9, 2017 at 7:2 Comment(3)
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.Tyler
All I needed to do was add "pug": "^2.0.3 under dependencies in package.json...Porcine
Life saver! For me this started to occur when compiling the server with webpack (I did __non_webpack_require__("pug").__express to preserve Node requireConsequent
T
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
Tiannatiara answered 27/7, 2017 at 5:56 Comment(5)
But I'm using the same version locally and globally.Geometrid
@HarshPatel I would suggest you to remove the "dependencies" from the package.json and re-install using command npm install --save express jade pugTiannatiara
Why would you install both jade and pug? Isn't jade just an older version of pug?Tabatha
@aboveyou00 My understanding is that Jade had to change its name (to Pug) because of a trademark dispute. I can't see a reason for installing them both.Atwood
This solution worked for me. Upvotted. (I did exclude "jade" from the command line but still...)Mulligrubs
B
6

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.

Benedix answered 22/1, 2020 at 21:35 Comment(0)
B
5

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

Bille answered 9/12, 2019 at 14:44 Comment(0)
T
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.

Teniers answered 23/5, 2019 at 13:48 Comment(0)
C
2

Run following Commands..

  1.npm remove pug express

  2.npm install pug express

This will solve the issue

Coverup answered 15/5, 2020 at 9:56 Comment(0)
A
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');


Alasteir answered 22/7, 2020 at 13:48 Comment(2)
This worked @Alasteir but please can you explain why it is needed for it to run smoothly? I checked other resources online but their explanations were not clearDiverge
@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 pugjs.org/api/getting-started.htmlAlasteir
M
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"
  }
Medicate answered 12/1, 2019 at 21:55 Comment(0)
Y
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'

Yugoslav answered 15/7, 2019 at 18:57 Comment(0)
C
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.

Colston answered 27/12, 2020 at 21:57 Comment(0)
E
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

Eastman answered 20/4, 2021 at 20:24 Comment(0)
G
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')

Goodwill answered 6/12, 2021 at 5:39 Comment(0)
N
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"
},
Nittygritty answered 25/7, 2022 at 20:12 Comment(0)
T
-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

Terrorist answered 3/9, 2018 at 23:25 Comment(0)
P
-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.

Parsimonious answered 13/5, 2021 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.