ReferenceError: moment is not defined in JavaScript [Fixed]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# Table of Contents

  1. ReferenceError moment is not defined in Node.js
  2. ReferenceError moment is not defined in the Browser
  3. Cannot find module 'moment' error

# ReferenceError: moment is not defined in JavaScript

The "ReferenceError: moment is not defined" error occurs for 2 reasons:

  1. Not installing and importing the moment package in a Node.js application.
  2. Not loading the moment script before your code in the browser.

referenceerror moment is not defined

# ReferenceError moment is not defined in Node.js

To solve the error in Node.js, make sure to install and import the moment package before using it.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install moment # ๐Ÿ‘‡๏ธ with YARN yarn add moment

npm install moment

If your project doesn't have a package.json file, you have to create one with the npm init -y command.
shell
npm init -y

issue npm init y command

Now you can use the moment library in your code.

index.js
import moment from 'moment'; console.log(moment().format());
The code for this article is available on GitHub

We used the ES6 module import/export syntax. If you use an older version of Node.js, use the require() syntax to import the module.

index.js
const moment = require('moment'); console.log(moment().format());

# ReferenceError moment is not defined in the Browser

To solve the error in the browser, make sure to load the script for the moment library before running your JavaScript code.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- ๐Ÿ‘‡๏ธ Your HTML code HERE ๐Ÿ‘‡๏ธ --> <!-- โœ… Load the moment library โœ… --> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer" ></script> <!-- โœ… Run your JavaScript code --> <script type="module" src="index.js"></script> </body> </html>

We loaded the script for the moment module before running our JavaScript code.

This way, we can be sure the moment variable will be available in our index.js file.

index.js
console.log(moment().format());

moment variable is available in index js

We can directly use the moment library in the index.js file without importing it.

# Cannot find module 'moment' error

To solve the error "Cannot find module 'moment'", make sure to install the moment package by opening your terminal in your project's root directory and running the following command: npm i moment and restart your IDE and development server.

cannot find module moment

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

shell
# ๐Ÿ‘‡๏ธ with NPM npm install moment # ๐Ÿ‘‡๏ธ with YARN yarn add moment

This will add the moment package to the dependencies of your project.

# Delete node_modules and reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.

If you use TypeScript and are getting the error "Cannot find module 'moment' or its corresponding type declarations", make sure to update your moment version to the latest. The issue is fixed in versions greater than 2.25.1.

shell
npm install moment@latest

# Make sure moment is in your dependencies object

If you still get the "Cannot find module 'moment'" error, open your package.json file and make sure it contains the moment package in the dependencies object.

package.json
{ // ... rest "dependencies": { "moment": "^2.29.1", } }

You can try to manually add the line and re-run npm install.

shell
npm install

Or install the latest version of the package:

shell
npm install moment@latest

The moment module should NOT be globally installed or be in your project's devDependencies. It should be in the dependencies object in your package.json file.

The code for this article is available on GitHub
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev