Harshal Sharma
Harshal Sharma

Reputation: 98

Unable to print date to json file

I'm writing code to print date directly from index.js to data.json but getting data is not defined error.

I did:

npm init

npm i jsonfile

index.js

const jsonfile = require('jsonfile');
const moment = require('moment');
       
const FILE_PATH = './data.json';

const DATE = moment().format();

const date = {

   date: DATE
 }

 jsonfile.writeFile(FILE_PATH, data);    //Error(here)
                              
 //ReferenceError: data is not defined

Upvotes: 0

Views: 54

Answers (2)

BDD
BDD

Reputation: 725

You never actually define the variable data, which is the issue. I think you want to write date to the file like this instead:

const jsonfile = require('jsonfile');
const moment = require('moment');

const FILE_PATH = './data.json';

const DATE = moment().format();

const date = {

   date: DATE
 }

 jsonfile.writeFile(FILE_PATH, date); // Change data to date

Upvotes: 0

Sachin Shah
Sachin Shah

Reputation: 4533

You have simple typo mistake just change date instead of data

const date = {

   date: DATE
}

jsonfile.writeFile(FILE_PATH, date); 

Upvotes: 1

Related Questions