Pug is typically used as a server-side HTML templating engine in Node.js and JavaScript web projects: you write templates in Pug's indentation-based, whitespace-significant syntax, and a build step or server runtime compiles them into HTML that is sent to the browser. It is not a browser-native language, not a client-side framework, and not a database tool; it is a source format for generating markup, most often inside an Express or similar Node.js application, or during a static-site build.
What Pug actually is
Pug began life as "Jade" and was renamed to Pug around 2015–2016, largely because of trademark concerns around the name "Jade." The rename matters for anyone reading older tutorials: Jade 1.x syntax is essentially Pug syntax, but package names, CLI names, and some API details differ. When you see jade.renderFile in an old Stack Overflow answer, that is the same idea as pug.renderFile in current packages. Versions and exact API behavior change over time, so the safe statement is the stable principle: Pug compiles a template string or file into an HTML string, a function, or a stream, depending on which API you call.
The core idea is simple. Instead of writing
Title
with closing tags, you write:
Indentation expresses nesting. Tags are implied by the first token, classes and IDs attach with . and #, and text follows the tag name or a pipe. Attributes go in parentheses: a(href="/about") About. This is the whole pitch: less punctuation, structure visible through indentation.
The dominant use: server-rendered pages in Node.js
The most common deployment is a Node.js web server that renders pages on the server and returns finished HTML. In Express, the typical setup is:
Then a route calls res.render('index', { title: 'Home', user }), and Express looks for views/index.pug, runs it through the Pug engine with the supplied locals, and sends the resulting HTML. This is the pattern most Pug documentation and tutorials assume, and it is why Pug is often described as "Express's default-style template engine," even though Express supports many engines and Pug is not built into Express itself.
The practical consequence is that Pug shines when the server knows everything needed to produce the page: a logged-in user's name, a list of products, a page title, flash messages. The template receives plain JavaScript values (locals), embeds them, and the browser receives ordinary HTML. No client-side JavaScript is required to make the page appear.
Templates, layouts, and composition
A second very common use is composition through include and extends/block. A layout.pug might define the shell:
and a page template does:
include partials/header.pug splices another template file in at compile time. This is how Pug projects typically avoid repeating navigation, footers, and form markup. Note the distinction: include is a compile-time file inclusion, while passing data is done through locals; they are separate mechanisms and beginners often conflate them.
Interpolation, escaping, and the safety question
Pug offers several ways to insert values, and the difference matters:
#{value} interpolates and HTML-escapes the value. !{value} interpolates unescaped, which is dangerous with untrusted input. = after a tag, as in p= user.bio, assigns an escaped value as the tag's text. != assigns unescaped.
The typical, safe pattern is to use escaped interpolation (#{} or =) for anything a user controls. Unescaped insertion is reserved for trusted, pre-sanitized HTML. This is the same escaping discipline any templating engine requires; Pug does not make injection impossible, it just makes the safe form the default in the common syntax. A frequent real-world bug is using != to "make formatting work" and thereby reintroducing cross-site scripting.
Conditionals, loops, and mixins
Pug embeds JavaScript control flow with a light syntax:
each (and its alias for) iterates; if/else if/else branch; unless is a negated if; case/when works like a switch. Mixins are reusable parameterized blocks:
Mixins are typically used for repeated UI atoms—buttons, form fields, cards—where a partial file would be overkill. They compile to functions, so they are cheap and can take arguments and even blocks.
Static site generation and build pipelines
Pug is also used outside a live server. Static site generators and custom build scripts compile .pug files to .html at build time, then deploy the HTML to a CDN or static host. Tools commonly paired with this are Gulp, Grunt, webpack loaders, and various SSG plugins; the specific plugin and its configuration are version- and tool-specific and should be checked against current documentation rather than assumed.
The typical build-time pattern is: watch .pug files, compile with locals and a data file (often JSON or a JS module), write .html output. This is popular for documentation sites, marketing pages, and prototypes where a full server is unnecessary. The trade-off is that anything dynamic must be baked in at build time or handled by separate client-side JavaScript.
Where Pug is not typically used, and why
Several adjacent uses are commonly assumed but are worth separating:
Not in the browser directly. Browsers do not understand Pug. You either render on the server or precompile to HTML or to a client-side template function. Shipping a Pug compiler to the browser is possible in principle but is not a typical production pattern; it adds weight and complexity for little benefit. Not a replacement for a component framework. Pug produces markup strings; it has no reactivity, no virtual DOM, no state management. In React, Vue, or Svelte projects, Pug is generally not used for the same components—those frameworks have their own template or JSX syntax. Pug sometimes appears in older AngularJS (ng-template) or Vue template lang="pug" setups, but that is a stylistic choice layered on the framework's own compiler, and support depends on the specific tooling version. Not a CSS preprocessor or a database layer. Confusion arises because Pug is often installed alongside Sass/Less and an ORM in the same project. They are independent tools.
A realistic small example
Putting the pieces together, a typical Express + Pug app looks like this. views/layout.pug provides the shell with block content. views/products.pug extends it and loops over products, using a mixin for each card. The route handler queries the database, passes products and title as locals, and calls res.render. The browser receives fully formed HTML; the database is never exposed to the template beyond the values passed in. This separation—data assembled in the route, presentation assembled in the template—is the structural convention most Pug codebases follow.
Ambiguities and what "typically" excludes
"Typically used" hides real variation. Pug usage in 2010s Express tutorials looked different from usage in a modern static build, and the language's own API has changed across major versions. If a question about Pug omits the surrounding stack (Express? a static generator? a specific framework integration?) and the Pug version, the precise answer is underdetermined: the stable answer is "as an HTML template compiler in a Node.js or JavaScript build/server context," while the specific API calls, plugin names, and configuration must be checked against the version and tool in use. Likewise, claims about Pug's popularity relative to EJS, Handlebars, or Nunjucks are time-sensitive and not something to assert without current data.
FAQ
Is Pug the same as Jade? Essentially yes for syntax; "Jade" is the former name, renamed to Pug around 2015–2016. Package and API names changed, so older code and tutorials use jade.
Can I use Pug with plain HTML files? You can include raw HTML in Pug with the pipe | for text or by using include on an HTML file in some configurations; the typical approach, though, is to write Pug and compile it.
Does Pug require Express? No. Express is the most common host, but Pug can render standalone via its own API or be compiled by build tools.
Is Pug safe against XSS? It defaults to escaping with #{} and =, but !{} and != bypass escaping. Safety depends on using the escaped forms for untrusted data.
Why does my Pug throw indentation errors? Pug is whitespace-significant; mixing tabs and spaces, or inconsistent indentation, is the usual cause.