Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference Error: moment is not defined (Using moment.js)

Tags:

momentjs

I installed the moment.js library and was about to play around with it a little bit, when i got this error. I've tried many different things, but nothing has worked. Any ideas?

I have a folder that i've installed moment.js into (npm install moment) and i'm running my program (node isDate.js).

import {moment} from 'moment';

function isDate(string) {
    {
        moment(string).format(["MM-DD-YYYY", "YYYY-MM-DD"]);
    }
} exports.isDate = isDate;

ReferenceError: moment is not defined

like image 545
Jason000 Avatar asked Jul 30 '19 03:07

Jason000


People also ask

How to solve the “ReferenceError moment is not defined” error?

To solve the "ReferenceError: moment is not defined" error in Node.js, make sure to install and import the moment package before using it. Copied! Note that if your project does not have a package.json file, you have to create one first with the npm init -y command.

How to fix moment is not defined in Node JS?

Not loading the moment script before your code in the browser. To solve the "ReferenceError: moment is not defined" error in Node.js, make sure to install and import the moment package before using it. Copied!

How to import moment in Node JS?

import {moment} from 'moment'; function isDate (string) { { moment (string).format ( ["MM-DD-YYYY", "YYYY-MM-DD"]); } } exports.isDate = isDate; worked for me. Try to use const moment = require ('moment'). Import syntax might not work in node without extra module.

Why can't I reference a document in Node JS?

Node is a server-side runtime so if you're on Node, you cannot reference the document object, which is an in-memory representation of the Document Object Model. That also includes when you're doing server-side rendered applications, which can sometimes have universal code that can run on both the server and the browser.


1 Answers

As per Aleks comment

const moment= require('moment') 

worked for me.

like image 63
patil Avatar answered Sep 28 '22 19:09

patil