Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot find module 'pug'

Tags:

node.js

pug

pugjs

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)

like image 547
Harsh Patel Avatar asked Jul 27 '17 05:07

Harsh Patel


People also ask

Can not find module HTML?

To solve the error "Cannot find module 'html-webpack-plugin'", make sure to install the html-webpack-plugin package by opening your terminal in your project's root directory and running the following command: npm i -D html-webpack-plugin and restart your IDE and dev server.


1 Answers

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!

like image 149
Sergi Nadal Avatar answered Oct 03 '22 09:10

Sergi Nadal