Search code examples
arraysnode.jsjsonfile-handlingyargs

Why do I keep getting an error saying I have unexpected end of JSON input while using nodeJS and yargs


Firstly, I am a noob to node.js and JavaScript.

I am trying to write a program that stores the notes of a user in the local system using yargs.

My train of through in the handler is as follows-

  1. First create the an object that contains the note's title and its contents.

  2. I assume that file is always created in the user's local system and is either going to be empty or have some notes.

  3. Get the contents of the file and if it is empty, create an object that has the array of all notes and push the note to it. I then write that to file after doing the necessary JSON manipulations.

  4. If an object is already present in the file, I just push the new note to it.

Here is my code

const yargs = require("yargs")
const fs = require("fs")


yargs.command({

    command: "add",

    describe: "Add a note",

    builder: {
        title: {
            describe: "Title of the note to add",
            demandOption: true,
            type: "string"
        },
        note: {
            describe: "Contents of the note to add",
            demandOption: true,
            type: "string"
        }
    },

    handler: function() {

        let fullNote = {
            title: yargs.argv.title,
            note: yargs.argv.note
        }

        let fileContents = JSON.parse(fs.readFileSync("notes.json").toString())

        if(fileContents === undefined || fileContents === null){
            let newArrayJSON = {
                notes: []
            }

            newArrayJSON.notes[0] = JSON.stringify(fullNote)

            fs.writeFileSync("notes.json", newArrayJSON)

        } else {
            fileContents.notes.push(JSON.stringify(fullNote))

            fs.writeFileSync("notes.json", newArrayJSON)
        }

    }
})

yargs.parse()

And this is the error message I get

PS D:\Documents\Projects\Node\Node-NotesApp> node app.js add --title="To Buy" --note="Eggs"
D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:1242
      else throw err
           ^

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.handler (D:\Documents\Projects\Node\Node-NotesApp\app.js:34:33)
    at Object.runCommand (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\lib\command.js:240:40)
    at Object.parseArgs [as _parseArgs] (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:1154:41)
    at Object.parse (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:599:25)
    at Object.<anonymous> (D:\Documents\Projects\Node\Node-NotesApp\app.js:54:7)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)

I have tried so many hings but cannot get rid of that error. Could someone help me. Any other helpful information/resources to help me better understand this concept would be greatly appreciated.

Any explanations/resources on working with JSON in node (like JSON.stringify, JSON.parse ) would be be very helpful.

Thanks in advance


Solution

  • I solved it by populating the file with an empty array.

    The error pops up when the file is empty as @GuyIncognito pointed out.

    Thanks everyone for the help