How to Fix Error EJS 'partial is not defined'?
Last Updated :
23 Jul, 2025
This article provides a detailed explanation of the ejs 'partial is not defined' error, detailing various approaches to resolve it, from using native EJS features to creating custom functions. The error 'partial is not defined' occurs in EJS when you use a partial function that hasn't been defined. EJS itself doesn't come with a built-in partial function. This function typically needs to be defined manually or is provided by a framework or middleware.
Below are the approaches by using these we can remove the error:
Using EJS includes Syntax
EJS has an include function that allows you to include other EJS files within your templates.
Syntax:
<%- include('path/to/partial') %>
Explanation:
- <%- is used to output the unescaped content, which is important for including other templates.
- include('path/to/partial') includes the content of the specified partial EJS file.
Steps to Implement:
Step1: Ensure the Partial File Exists:
- Create a partial file, e.g., views/partials/header.ejs.
Step2: Use the include Keyword in Your Main Template:
- In your main EJS file (views/index.ejs):
<body>
<%- include('partials/header') %>
<!-- Other content -->
</body>
Creating a Custom partial Function
You can define your own partial function in your Node.js application to render partials.
Syntax:
app.locals.partial = function(name) {
return fs.readFileSync(__dirname + '/views/' + name + '.ejs', 'utf8');
};
Explanation:
- app.locals.partial creates a global function available in all EJS templates.
- fs.readFileSync reads the content of the partial file synchronously.
Steps to Implement:
Step1: Define the Function in Your app.js:
import express from 'express';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.locals.partial = function(name) {
return fs.readFileSync(path.join(__dirname, 'views',
'partials', name + '.ejs'), 'utf8');
};
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.get('/', (req, res) => {
res.render('index', { title: 'Home Page' });
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Step2: Use the Custom partial Function in Your EJS Template:
<body>
<%- partial('header') %>
<!-- Other content -->
</body>
Using Middleware or Helper Functions in Express.js
Express.js can be configured to use helper functions for rendering partials.
Syntax:
res.render('view', {
partial: function(name) {
return fs.readFileSync(path.join(__dirname, 'views', name + '.ejs'), 'utf8');
}
});
Explanation:
- Adding a helper function to the render context allows for dynamic partial inclusion.
Steps to Implement:
Step1: Define the Helper Function within Your Route Handler:
import express from 'express';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.get('/', (req, res) => {
res.render('index', {
title: 'Home Page',
partial: function(name) {
return fs.readFileSync(path.join(__dirname, 'views', 'partials', name + '.ejs'), 'utf8');
}
});
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Step2: Use the Helper Function in Your EJS Template:
<body>
<%- partial('header') %>
<!-- Other content -->
</body>
Conclusion
The 'partial is not defined error' in EJS can be resolved by using different methods depending on your application's needs. The simplest approach is to use the built-in EJS include syntax. For more flexibility, you can define a custom partial function within your application, either globally or within specific routes. This article provides detailed steps to implement each approach using ES6 module syntax-. By following these instructions, you can effectively manage your partial templates and ensure your EJS views render correctly.
Similar Reads
Advanced JavaScript Backend Basics Getting Started with JavaScript Backend Basics The following article is a continuation to my previous article. In this article, we will dive deep into some of the advanced JavaScript concepts that are being used widely in the industry. "==" vs "===" The "==" coerces the types of the input that is it
5 min read
What is backend Developer Backend developers are a crucial part of modern web development teams, responsible for building the server-side logic and infrastructure that powers web applications. In this comprehensive article, we'll see the role of a backend developer, covering various aspects such as responsibilities, skills r
5 min read
What is Backend Development? Skills, Salary and Roles Backend development is a blessing to all of us that we are able to get everything done by just sitting at home. Be it booking tickets, watching movies, or any sort of thing, backend development plays a major role in building an application. It is also one of the highly demanding professions in the I
7 min read
Advantages and Disadvantages of JavaScript JavaScript is an interpreted programming as well as a scripting language. Many of these are related to the way, JavaScript is often executed directly in a client's browser commonly utilized in web development. It was originally developed by Netscape as a way to feature dynamic and interactive elemen
3 min read
Difference between Node.js and JavaScript JavaScript and Node.js are both crucial in modern web development, but they serve different purposes and are used in different environments. JavaScript is a programming language primarily used for client-side web development, while Node is a runtime environment that allows JavaScript to be executed
3 min read
How to Use Node.js for Backend Web Development? In the world of website design nowadays, NodeJS is a supportive tool for making capable backend systems. Whether you are an experienced web designer or just beginning out, NodeJS can upgrade your aptitude and help you in building extraordinary websites. This guide will show you how to use NodeJS to
8 min read
Top 10 Backend Technologies You Must Know To provide any responsive and effective software solution, frontend, and backend are the two most essential technologies that work together. A back-end framework is used to create server-side web architectures stably and efficiently. Backend technologies focus on improving the hidden aspects of the
11 min read
10 Best Backend JavaScript Frameworks in 2025 When JavaScript was first created, it used to run only in the browser of the client (user). At that time, it was sufficient, as websites used to be static and just displayed some information for the user to see and read. But as both time and technology moved forward, web development changed. Soon, t
11 min read
Good Coding Practices For Backend Developers API, Authentication, Design Patterns, MVC, Cache, Cookies, Sessions, JSON, Endpoints, Server, Hosting, Postman, CRUD, Curl...Aren't all these words familiar to you???If yes, then surely you're a backend developer or working on the backend part of the application. Clients often underestimate the work
9 min read
12 Backend Development Tools For Web Developers While Frontend Web Development is concerned with the designing of the user interface of the website using web technologies like HTML, CSS, JavaScript, etc. - Backend Web Development (or you can say Server-Side Development) is responsible for the appropriate functioning of the website.In simple words
9 min read