Jade Template Engine: Use Pug in Node.js

An older Jade example can leave you wondering which package to install and whether its syntax still works. Jade is the former name of Pug, a Node.js template engine that uses indentation to describe nested elements and renders them as HTML.

I’ll show you which package to install and how to render a Jade-style template with Node.js, including its Express view setup.

TL;DR

Jade is the former name of Pug, an indentation-based Node.js template engine. For new work, use the pug npm package and treat older Jade examples as historical references that may need migration.

  • Install Pug in the project with npm install pug.
  • Write nested elements by indentation and use template locals for dynamic values.
  • Express selects Pug with app.set(‘view engine’, ‘pug’) and renders a view with res.render().

What is the Jade template engine?

Jade is the historical name of Pug, a Node.js template engine that combines templates with data on the server and returns HTML for an application to send to a browser.

TermWhat it refers toWhat to use
JadeThe former project name and syntax used by older examplesRead it as historical context
PugThe renamed template engine and modern packageInstall pug from npm
HTMLThe markup produced when a template is renderedInspect the rendered response in the browser or test

HTML spells out opening and closing tags, while Pug defines nesting through indentation. A line indented beneath another becomes its child, so changing indentation changes the document structure even when the tag name stays the same. I ran the direct Node render shown here to examine HTML before connecting the template to Express.

The supplied research does not establish one best Express template engine for every project. Choose Pug when its syntax fits your app, or serve fixed pages as HTML with CodeForGeek’s guide to rendering an HTML file in Express.

How to use Jade syntax with Pug in Node.js

Install Pug locally, write a .pug template, then compile it with data and inspect the HTML it returns.

Step 1: Install the Pug package

Run the install from the root of a Node.js project. This test command uses a temporary local install rather than adding a dependency to an application manifest.

npm install --no-save pug

In this isolated workspace I ran the command above. npm reported:

up to date, audited 104 packages in 6s

33 packages are looking for funding
  run `npm fund` for details

1 high severity vulnerability

To address all issues, run:
  npm audit fix

Run `npm audit` for details.
Terminal output after installing the Pug package

npm reported a high-severity finding in the tree it audited, but the summary did not identify the package or prove Pug was affected. Read the full audit report from the application before changing dependencies. The suggested npm audit fix can update packages, so test proposed changes against the app before deployment.

For an application, use the regular npm install command so Pug is saved in package.json, and add Express for the route example. Commit the lockfile to pin the resolved dependency tree. This no-save check captures only the isolated Pug installation.

Step 2: Write a template with indentation

The template below declares a page title, a heading and a list of items supplied as local data. It also sets a language attribute on the root HTML element.

doctype html
html(lang="en")
  head
    title= pageTitle
  body
    h1= heading
    ul
      each item in items
        li= item

The each loop emits one item per array value in order, or none when the array is empty. Add a conditional if the page needs a message for that case, and supply locals whose names match the template.

Syntax caseHow it works
AttributesParentheses attach attributes to a tag. Values can be literal text or JavaScript expressions, and are escaped by default.
Class valuesClasses can be a string, an array of names, or an object that turns class names on or off.
Boolean attributesWith an HTML doctype, a true value renders a boolean attribute and false omits it.
Attribute namesQuote names containing characters that would interfere with JavaScript expression syntax.
Plain textPrefix a line with a pipe when it should be text without a new tag.
Multiline textA period after the tag starts a text block. Indentation keeps following lines within that block.
Class and IDA dot sets a class and a hash sets an ID. If the tag name is omitted, Pug uses a div.
ImagesProvide a source URL and alternative text as attributes so the rendered element remains understandable when the image cannot be seen.

Normal text interpolation is escaped by default. The raw form emits HTML directly, so reserve it for trusted, sanitized content and never reuse it in URL or script attributes.

p Hello, #{name}
p= name
p!= trustedHtml

Pug removes indentation and whitespace between elements but preserves spaces inside text, which explains the compact HTML in the terminal output. Add explicit spacing around inline tags when the visible text needs a separator. See Pug’s plain-text reference for whitespace controls.

Set the lang value to the page’s primary language. If the route selects localized content, pass the matching language as a local rather than hard-coding a value that disagrees with the text. Validate link and image destinations passed by the route because HTML escaping protects markup, not allowed URL schemes.

Step 3: Render data into HTML

Compile the template into a rendering function and pass an object whose keys match the template’s local names. The following named source file contains this example and prints the returned HTML.

const pug = require('pug');

const template = `
doctype html
html(lang="en")
  head
    title= pageTitle
  body
    h1= heading
    ul
      each item in items
        li= item
`;

const render = pug.compile(template);
const html = render({
  pageTitle: 'Template example',
  heading: 'Hello from Pug',
  items: ['Jade syntax', 'Pug package']
});

console.log(html);

Run the file with node jade_template_demo.js. This is the captured output:

<!DOCTYPE html><html lang="en"><head><title>Template example</title></head><body><h1>Hello from Pug</h1><ul><li>Jade syntax</li><li>Pug package</li></ul></body></html>
Terminal output from rendering a Jade-style Pug template

Compile returns a function for the template source. Keep that function when rendering the same template with different locals, rather than compiling the source for each data object.

Keep request-specific data out of shared mutable state so concurrent requests cannot reuse another request’s values.

The doctype in the template becomes the HTML doctype in the output. The root language attribute also carries through as lang=”en”.

Step 4: Connect Pug to Express

Put index.pug in Express’s configured views directory, set Pug as the view engine at startup, then render the view by name with request-specific values.

const express = require('express');
const app = express();

app.set('view engine', 'pug');
app.set('views', './views');

app.get('/', (req, res) => {
  res.render('index', {
    pageTitle: 'Home',
    heading: 'Hello from Express',
    items: ['First item', 'Second item']
  });
});

const server = app.listen(0, async () => {
  const { port } = server.address();
  const response = await fetch(`http://127.0.0.1:${port}/`);
  console.log(await response.text());
  server.close();
});

Run jade_express_demo.js to start Express on a temporary local port and request the route. It renders the Pug view and prints the response HTML below.

<!DOCTYPE html><html lang="en"><head><title>Home</title></head><body><h1>Hello from Express</h1><ul><li>First item</li><li>Second item</li></ul></body></html>
Terminal output showing Express rendering a Pug view

The demo closes its temporary server after that request. For a broader comparison, see CodeForGeek’s Express views guide for EJS.

Express can cache the compiled view template in production, but it does not cache each rendered response.

Set the views directory explicitly when an app may start from different working directories. Resolve the path from the application entrypoint so a process manager does not change where Express looks for templates.

What should you check when using Jade syntax?

This table maps common symptoms to the first place to inspect.

CaseWhat you may seeWhat to check
Nested markup renders incorrectlyA child appears at the wrong level or parsing failsCheck indentation under each parent and keep sibling indentation consistent
Template local is blank or missingA value expression has no matching dataPass the expected key in the render locals object
User text appears as markupAn unescaped expression emits HTMLUse escaped interpolation and sanitize any value before deliberately emitting raw HTML
An include cannot resolveThe renderer reports a missing template fileCheck the relative path from the including template, or set the configured base directory for absolute paths
Express cannot find a viewThe route returns a missing-view errorCheck the configured views path and the name passed to render

Pass only fields the page displays, not a full database record that may contain private data. Keep data loading in the route and templates focused on presentation so those failure sources stay separate.

An include places a shared header, navigation bar or footer at that point in the template, resolving relative paths from the source view.

A mixin is a function-like reusable block that accepts arguments, so repeated cards or links can vary by label or target without copied markup.

  • Use the pug package for new work. The .jade extension still works, but the project marks it deprecated in favor of .pug.
  • Use the official Pug migration guide for syntax and removed API changes, especially if custom code extends compiler, lexer or parser classes. Compare converted views with their intended output before replacing them.
Older Jade formPug v2 check
Quoted interpolation inside an attributeUse a JavaScript expression for the attribute value, or build the string explicitly.
Legacy mixin call with no call prefixUse the current plus-prefixed mixin call form.
Each or for statement with a leading dashRemove the dash and use the Pug loop statement directly.

An if/else branch renders one path from a condition. Use unless when a branch should appear for a negative case.

Use render for a source string or renderFile for a path. The API deprecates pretty output because added whitespace can affect how a template interprets whitespace. Inspect generated HTML instead of relying on that formatting option.

Conclusion

Pass locals explicitly and inspect the rendered HTML before adding the view to a route.

For syntax and API details, use the Pug documentation, its API reference and Express template-engine guide.

FAQ

These answers cover the naming and setup questions that come up when an older Jade tutorial is used with a current Node.js project.

Is Jade the same as Pug?

Jade was renamed to Pug. Pug is the current project and the official package name from version 2 onward. Older Jade code may need migration because the major-version transition included syntax changes and removals.

What package should I install for Jade templates?

Install the pug package with npm install pug. The Jade name belongs to older documentation and package examples.

How does Jade differ from HTML?

HTML writes opening and closing tags directly. Jade-style Pug templates use indentation to describe nested elements and compile that structure into HTML.

Can I use Pug with Express?

Yes. Set app.set(‘view engine’, ‘pug’), place templates in the configured views directory, and render a view with res.render() while passing its local data.

Can I keep a .jade file extension?

The Pug migration guide says the .jade extension remains supported but is deprecated. Use .pug for new templates and review old syntax for migration changes.

Can I use Pug without Express?

Yes. Pug’s Node.js API can render templates on its own. Express is only needed when an app uses its view-engine integration for HTTP routes.

Aditya Gupta
Aditya Gupta

Aditya Gupta is a founding member and editor at CodeForGeek. He first found his way into tech by reading articles, and now writes approachable guides to Node.js security, authentication, AI tools, coding agents, and web scraping.

Articles: 529