Error: Cannot find module 'pug'

33.2k Views Asked by At

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)

15

There are 15 best solutions below

0
On

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

0
On

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.

2
On

Install

npm i pug

Put

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

before

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


5
On

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
0
On

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

3
On

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!

0
On

Run following Commands..

  1.npm remove pug express

  2.npm install pug express

This will solve the issue

0
On

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
On

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

0
On

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
On

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
On

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"
},
0
On

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.

0
On

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.

0
On

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.