In Express.js, app.locals is an object of application-level local variables. Values you assign to it are available in every template rendered with res.render() (and app.render()) for the life of the process.
Use it for shared site data (title, contact email) or helper functions. In middleware, read the same object as req.app.locals. Per-request data belongs on res.locals instead.
Syntax
Read app.locals in your Express.js route handler. Start with this line.
Express
app.locals // Object
app.locals.name = value
req.app.locals.name // same object in middleware
Call
What it does
app.locals // Object
Primary signature.
app.locals.name = value
Related call form.
req.app.locals.name // same object in middleware
Related call form.
Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.
Library declarations
Express
const express = require("express");
const app = express();
app.locals // Object
app.locals.name = value
req.app.locals.name // same object in middleware
Parameters
Parameter
Description
Strings / numbers
Required. Site name, version, contact email
Objects
Required. Grouped config for layouts
Functions
Required. Helpers callable from templates
Return Value
app.locals is an object you read and write — not a function call. Assign properties on it; templates and handlers see those values. There is no separate “return” beyond the object itself.
Express
app.locals.title = 'CodeToFun';
app.locals.year = 2026;
// In a view / handler:
res.locals.page = 'home'; // request-scoped overlay
// app.locals.title stays available app-wide
Express.js Examples
Example 1 — Site Title in Views
Official-style assignments available to every res.render.
views/hi.ejs
<h1><%= site %></h1>
<p>Hello, <%= user %></p>
GET /hi?name=Ada
→ <h1>CodeToFun</h1>
<p>Hello, Ada</p>
GET /hi
→ Hello, Guest (site still CodeToFun)
How It Works
App scope.site is set once on app.locals.
Request scope.user is set on res.locals inside the handler.
Both appear in the view. The template can use site and user together.
Next request.user resets; site stays CodeToFun.
Example 3 — Helper on app.locals
Attach a function templates (and middleware) can call — same idea as the docs’ helper pattern.
Console: HI
views/home.ejs
<p><%= upper(message) %></p>
GET /
→ <p>WELCOME</p>
How It Works
Store a function.app.locals.upper is a shared helper.
Use in middleware.req.app.locals is the same object.
Use in the template. EJS can call upper(message) while rendering.
Keep keys trusted. Helper names are fixed by your code, not by users.
Compatibility
Runtime Support
app.locals is an Express.js application property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only see rendered HTML.
✓ Server · Express 4 & 5
app.locals
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
app.localsExpress on Node.js
Bottom line: Set locals in Node.js, configure a view engine, then run the app with node and open a rendered page.
Frequently Asked Questions
It is an object of application-level local variables. Properties you set on it are available in templates rendered with res.render for the life of the app process.
app.locals lasts for the whole application. res.locals lasts only for one request/response cycle.
Use req.app.locals (or res.app.locals). That is the same object as app.locals.
Yes. Express documents this pattern — for example attaching a formatting function that templates can call.
No. It is server-side data for Express templates. Browsers only receive the rendered HTML.