Last modified: Jun 23, 2026
Fix Cannot find module 'dayjs' in Node.js
Getting the error "Cannot find module 'dayjs'" in Node.js can be frustrating. This error usually means the dayjs package is missing or not installed correctly. But don't worry. This guide will help you fix it quickly and clearly.
We will cover common causes, step-by-step solutions, and examples. By the end, you will understand how to install and use dayjs without errors. Let's dive in.
What Is the "Cannot find module 'dayjs'" Error?
This error occurs when Node.js cannot locate the dayjs package in your project. It typically looks like this:
Error: Cannot find module 'dayjs'
Require stack:
- /home/user/project/app.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/user/project/app.js:1:13)
The error means your code tries to load dayjs, but Node.js can't find it in the node_modules folder. This is a common issue for beginners.
Common Causes of the Error
Several reasons can cause this error. Understanding them helps you fix it faster.
1. Missing Installation – You forgot to run npm install dayjs.
2. Wrong Directory – You installed dayjs in a different folder than your project.
3. Corrupted node_modules – The package might be broken or incomplete.
4. Global vs Local – You installed dayjs globally but tried to use it locally.
5. Package.json Issues – The package is listed but not installed properly.
How to Fix the Error Step by Step
Let's solve the problem with clear steps. Follow each one carefully.
Step 1: Install dayjs Locally
The most common fix is to install dayjs in your project directory. Open your terminal and navigate to your project folder. Then run:
npm install dayjs
This command adds dayjs to the node_modules folder and updates your package.json file. After installation, try running your code again.
Step 2: Check Your Working Directory
Make sure you are in the correct folder when running your script. Use pwd (Linux/Mac) or cd (Windows) to verify. If you installed dayjs in one project but run the script from another, the error will appear.
Always run npm install inside the folder that contains your package.json file.
Step 3: Delete and Reinstall node_modules
Sometimes the node_modules folder gets corrupted. Delete it and reinstall all dependencies. Run these commands:
rm -rf node_modules
npm install
This forces a clean installation. After that, the dayjs module should be available.
Step 4: Use the Correct Import Syntax
Make sure you import dayjs correctly. In Node.js with CommonJS, use require. For ES modules, use import. Here is an example:
// CommonJS (default in Node.js)
const dayjs = require('dayjs');
// ES modules (if you set "type": "module" in package.json)
// import dayjs from 'dayjs';
If you use the wrong syntax, Node.js might not find the module. Stick to one style per project.
Step 5: Check package.json
Open your package.json file and look under dependencies. You should see something like:
{
"dependencies": {
"dayjs": "^1.11.10"
}
}
If it's missing, run npm install dayjs --save to add it. If it's there but the error persists, try reinstalling as in Step 3.
Example: Using dayjs After Fixing
Once the error is fixed, you can use dayjs to format dates. Here is a simple example:
// Import dayjs
const dayjs = require('dayjs');
// Get current date and time
const now = dayjs();
// Format the date
const formatted = now.format('YYYY-MM-DD HH:mm:ss');
console.log(formatted); // Output: 2025-03-15 14:30:00
Output:
2025-03-15 14:30:00
This shows that dayjs is working correctly. No more module errors.
Additional Troubleshooting Tips
If the error still appears, try these extra tips.
Use the Full Path – In rare cases, specify the full path to the module:
const dayjs = require('./node_modules/dayjs');
But this is not recommended for production. It's better to fix the root cause.
Check Node.js Version – Older Node.js versions might have compatibility issues. Update to the latest LTS version. Run node -v to check.
Clear npm Cache – Sometimes npm cache causes problems. Run this command:
npm cache clean --force
Then reinstall dayjs.
Look for Typos – Double-check your import statement. A simple typo like dayj instead of dayjs can cause the error.
Preventing the Error in the Future
To avoid this error again, follow these best practices.
Always Use package.json – Track your dependencies. Run npm init to create one if you haven't.
Install Locally – Use npm install without the -g flag for project-specific packages.
Commit node_modules? – Do not commit node_modules to version control. Instead, run npm install after cloning a project.
Use a Lock File – The package-lock.json ensures consistent installations across environments.
For more help with Node.js module errors, check out our guide on how to Fix Node.js Error: Cannot find module. It covers similar issues for other packages.
Conclusion
The "Cannot find module 'dayjs'" error is easy to fix. Install the package locally, check your directory, and use the correct import syntax. If problems persist, delete and reinstall node_modules or clear the npm cache.
By following this guide, you can resolve the error and start using dayjs for date manipulation. Remember to always check your package.json and work in the right folder. Happy coding!