Last modified: Jun 23, 2026

Fix Cannot find module 'moment' Error

Encountering the error Cannot find module 'moment' in Node.js is common. It stops your app from running. This guide explains why it happens and how to fix it fast.

We will cover installation, path issues, and package manager conflicts. You'll get clear steps and code examples.

Why This Error Occurs

Node.js cannot locate the moment package in your project. This usually means the module is missing or installed incorrectly.

Common reasons include:

  • You forgot to install moment.
  • The module is installed globally, not locally.
  • Your package.json is missing the dependency.
  • You are running the script from the wrong directory.
  • A corrupted node_modules folder.

Let's solve each case.

Step 1: Install the moment Package

First, ensure moment is installed in your project. Run this command in your project root:


npm install moment

This adds moment to your node_modules folder and updates package.json.

After installation, test with a simple script:


// app.js
const moment = require('moment');

const now = moment().format('YYYY-MM-DD');
console.log('Current date:', now);

Run it:


node app.js

Expected output:


Current date: 2025-03-26

If you still see the error, move to Step 2.

Step 2: Check Your Working Directory

Node.js looks for modules in the node_modules folder of the current directory. If you run your script from a different folder, the module won't be found.

Always run your script from the project root where package.json lives.

For example, if your project is at /home/user/myapp, do this:


cd /home/user/myapp
node app.js

If you run it from /home/user, Node.js won't find moment.

Step 3: Delete node_modules and Reinstall

A corrupted node_modules folder can cause this error. Delete it and reinstall all dependencies:


rm -rf node_modules
npm install

This forces a fresh install. Then run your script again.

Step 4: Use npm ci for Consistent Installs

If you use a package-lock.json file, use npm ci instead of npm install. It is faster and ensures exact versions.


rm -rf node_modules
npm ci

This avoids version mismatches that can break module resolution.

Step 5: Check Global vs Local Installation

If you installed moment globally with npm install -g moment, your local script cannot access it. Node.js does not look in global modules by default.

Always install packages locally:


npm install moment --save

The --save flag adds it to package.json automatically.

Step 6: Verify package.json

Open your package.json and check the dependencies section. It should have:


{
  "dependencies": {
    "moment": "^2.29.4"
  }
}

If it's missing, add it manually and run npm install.

Step 7: Use a Different Module (Optional)

The moment package is in maintenance mode. Consider using dayjs or date-fns as modern alternatives. They are smaller and faster.

To use dayjs, install it:


npm install dayjs

Then update your code:


const dayjs = require('dayjs');
const now = dayjs().format('YYYY-MM-DD');
console.log('Current date:', now);

This avoids the moment error entirely.

Common Mistakes and Solutions

Mistake 1: Running npm install without a package.json file. Create one with npm init -y first.

Mistake 2: Using a different package manager like Yarn or pnpm. If you use Yarn, run yarn add moment instead.

Mistake 3: Typing the module name wrong. Double-check spelling: moment not moments or momentjs.

Related: Fix Other Module Errors

If you encounter similar errors for other packages, read our guide on how to Fix Node.js Error: Cannot find module. It covers general steps for any missing module.

Conclusion

The error Cannot find module 'moment' is easy to fix. Install the package locally, check your directory, and reinstall dependencies if needed.

Always use npm install moment --save and run your scripts from the project root. If problems persist, delete node_modules and reinstall.

Consider switching to dayjs for a modern solution. This keeps your code lightweight and future-proof.

Now you can run your Node.js app without the error. Happy coding!