How to Resolve Error: Cannot find module 'ejs' ?
Last Updated :
24 Apr, 2025
EJS (Embedded JavaScript) is the templating engine for JS that simplifies the task of generation of HTML markup with embedded JS code. It allows you to embed dynamic content and logic directly within the HTML templates, which makes it easier to generate dynamic web pages in Node.js Apps. While working with this engine, developers usually face the Cannot find module 'ejs' error. In this article, we will learn how to fix or resolve this error.
What is the "Cannot find module 'ejs'" Error?
The Cannot find module 'ejs' error occurs when the Node.js application cannot locate the required ejs module in the application. Some possibilities and occurrence scenarios encounter this error while trying to use ejs module. This error suggests that this module is not installed or it may be named differently. It is important to verify the correct installation of the required module and also ensure that the module's name matches correctly.
Error Sample:

Why does "Cannot find module 'ejs'" occur?
Below, are the reasons for "Cannot find module 'ejs'" occurring:
- Module Not Installed
- Incorrect Module Name
Reason 1: Module Not Installed
In this scenario, Node.js script uses the 'ejs' module to read an EJS template from 'index.ejs', compiles it, replaces a variable with the value 'World', generates HTML, and writes it to 'output.html'. But, as the module is not installed, it will give the Error as "Cannot find module 'ejs'". Without installing the module, we cannot use ejs.
JavaScript
const ejs = require('ejs');
const fs = require('fs');
const template = fs.readFileSync('index.ejs', 'utf-8');
const compiledTemplate = ejs.compile(template);
const data = { name: 'World' };
const renderedHTML = compiledTemplate(data);
fs.writeFileSync('output.html', renderedHTML);
console.log('HTML file generated successfully!');
Output:
node:internal/modules/cjs/loader:998
throw err;
^
Error: Cannot find module 'ejs'
Reason 2: Incorrect Module Name
Another reason for the error mighr be a typing mistake or incorrect naming when trying to import the ejs module. Node.js is case-sensitive, so ensure that the module name is spelled correctly.
JavaScript
const ejs = require('EJS'); // Incorrect
Output:
node:internal/modules/cjs/loader:998
throw err;
^
Error: Cannot find module 'EJS'
Below are the solutions for "Cannot find module 'ejs'" Error:
Approach 1: Install ejs Module
We can resolve this error by installing the ejs module externally using npm package manager. Execute the below command in the terminal to install the module.
Command:
npm install ejs
Once we execute the command, it will take a couple of minutes to completely install the package according to the internet speed.

Once the installation is completed, try to import the package and verify whether the error is resolved or not. We can also check by verifying the version of ejs using below command.
npm show ejs version

Approach 2: Correct the Spelling Mistake
Sometimes, the error occurs due to the spelling mistake while importing the module. So, we can correct the spelling mistake while importing this module.
const ejs = require('ejs'); // Correct
By following these approaches the will definitely be resolved.
Similar Reads
How to resolve a "Cannot find module" error using Node.js?
This article outlines the steps you can take to troubleshoot and fix "Cannot find module" errors in your Node.js projects. These errors typically arise when Node.js cannot locate a module you're trying to use in your code. Table of Content Error "Cannot find module" Approach to Solve the ErrorInstal
3 min read
How to Fix The Module Not Found Error?
In this article, we are going to cover topics related to ' Module Not Found Error' and what the error means. the reason for the occurrence of this error and how can we handle this error. What is "ModuleNotFoundError"? A "ModuleNotFoundError" is a common error message in programming, particularly in
5 min read
How to Resolve npm Command Not Found Error in Node.js
Node Package Manager (npm) is the default package manager for NodeJS. It allows developers to easily manage libraries and dependencies in their NodeJS projects. It is an essential tool for managing the installation and versioning of packages from the npm registry.The npm: command not found error hap
4 min read
How to Fix "npm ERR! code ENOENT" Error?
You will see the error message ânpm ERR! code ENOENT syscall openâ if you try to run an npm command outside your project root folder. To resolve this error, first, make sure you are in your projectâs root directory before running the command or generating a new package.json file in that directory. n
2 min read
How to Fix Error EJS 'partial is not defined'?
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.
3 min read
How To Create Modules in NodeJS?
Modules are the building blocks of NodeJS code. They help you break down your project into smaller, manageable pieces, each with its own job.To create a module, just make a JavaScript file and export what you want to share. Other files can then import and use those exports, adding that functionality
3 min read
How to Fix the "partial is not defined" Error in EJS ?
Encountering an error message like "partial is not defined" can be a stumbling block when you're working with EJS (Embedded JavaScript) templates. This error typically pops up when you're trying to use the `partial` function in EJS, but it's not being recognized. Don't worry, though it's a common hi
3 min read
How to solve npm error npm ERR! code ELIFECYCLE ?
In order to solve the "npm ERR! code ELIFECYCLE " error which is a very common type of error that occurs during npm operation on our command prompt or terminal such as installing npm or an npm package, follow the steps given below : Terminal output of the error :Fixing common NPM errors like npm ERR
2 min read
How to remove all Global Modules in Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to remember that Node.js is not a framework and itâs not a programming language. Most of the people are confused and understand itâs a framework or a programming language. We
2 min read
How to use External Modules and NPM in a project ?
Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into
3 min read