pug Getting started with pug Installation


To install the Pug template rendering system, follow these steps:

  1. Have the Node.js environment installed on your machine
  2. Run npm install pug --save to install the pug module to your current project.

You can now use pug in your project through the standard require mechanism:

const pug = require("pug");

If you are using Express in your application, you do not need to require("pug"). However, you must set the view engine property of your Express application to pug.

app.set("view engine", "pug");

Further, you must set the view directory of your app so that Express knows where to look for your Pug files (for compilation).

app.set("views", "path/to/views");

Within your Express route, you can then render your Pug files by calling the res.render function with the path of the file (starting from the directory set by the app.set("views") option).

app.get("/", function (req, res, next) {
    // Your route code
    var locals = {
        title: "Home",
    res.render("index", locals);

In the above, index points to a file located at views/index.pug, and locals represents an object of variables that are exposed to your file. As will be explained in later sections, Pug can access variables passed to it and perform a variety of actions (conditionals, interpolation, iteration, and more).