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
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.
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!
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.
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.
As per Aleks comment
const moment= require('moment')
worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With