1

For this bot, I have a JSON file with all user info in it; it's formed properly; that's not the problem...

I also have the bot running on a server, so it can run 24/7; but I turn it off there when I work on it on my laptop. I keep my code synced using a GitHub repo, and i push/pull my changes frequently. That's not the issue either...

The problem is, when I run it from my laptop, all is well. The bot works like it should with no issues; but when I run it from the server, pulling my code from the bot's repo so everything's the exact same as what's on my laptop, I keep getting an "Unexpected end of JSON input" error anytime I try to use a specific command.

Reading the error, it shows that the error isn't the command itself, but a function from another file it's calling using module.exports, specifically where that file reads the JSON file containing user info.

I'm using the following line of code to parse the JSON (both on the laptop, and server):

let CycleSettings = JSON.parse(fs.readFileSync("./nicknames.json", "utf8"));

The error is as follows (server-side):

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.StartCycle (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/cycling.js:6:34)
    at Object.execute (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/commandsFolder/cycle.js:84:21)
    at Client.<anonymous> (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/index.js:105:17)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (/home/hostingmain/Desktop/Discord Bots/Nick-Cycle/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)

What's weird is this error only happens when the bot is running server-side, and when I take it down there, and fire it back up on my laptop, no issues.

I also have 2 other bots running on the same server, but neither of them give me this issue.

To verify that the code is the same; after pulling my repo, I am capable of using everything I had just made on my laptop, on the server (new commands, response tweaks, feature adjustments, etc.); but the same error occurs from the same command, for the same reason, but only on the server.

What's weirder is that this wasn't always a problem. About a month ago, when i got this particular feature working; everything worked fine, both on my laptop and the from the server. I haven't changed the function file since.

What's even weirder is that it's not 100%. Occasionally I'm able to use the buggy command successfully, but those situations are few and far between, and completely random from what I can tell.

I've got absolutely no clue what's causing it, and I've got no clue where to start looking... Any help is appreciated...

UPDATE: I've reinstalled node.js (the latest version) on both the server, and my laptop. None of the code has changed, and the error still doesn't appear on my laptop, but it does on the server. Again, the code is the exact same at both places.

EDIT: Here's the format of the JSON I'm trying to parse: (Note, I've replaced ID's and values with placeholders to illustrate the format of the JSON without actually disclosing user information)

{
     "Discord Server ID" {
          "Name": "Name of the Discord server (for my reference)",
          "users": {
               "Discord User ID": {
                    "nicknames": ["1", "2", "3"],
                    "interval": 300,
                    "started": false
               }
          }
     }
}
6
  • 1
    Can you provide a snippet of the buggy code/error logs? Also, what server are you using, and are they using the same node version? Commented Jul 26, 2020 at 2:25
  • Basically, if the same code is running ok on somewhere and not working on elsewhere, obviously the environment or other factors required for code is not there as required. In your case, it looks like your source JSON is invalid in the server but correct on your laptop. Use this online validator to check if JSON on your server-side is valid. jsonformatter.org Commented Aug 12, 2020 at 4:14
  • added the error message @theusaf and the line of code that the error happens at. Commented Aug 12, 2020 at 4:16
  • @PubuduSitinamaluwa I just used that site you gave me, and it said my json is valid. Commented Aug 12, 2020 at 4:24
  • Can you share the JSON you are parsing? Commented Aug 12, 2020 at 5:25

1 Answer 1

1

Reading the error, it shows that the error isn't the command itself, but a function from another file it's calling using module.exports, specifically where that file reads the JSON file containing user info.

Obviously the solution is to log the contents of the JSON file after reading it with something like require('fs').readFileSync before trying to parse it as JSON. With the information you've given, this is the next step to take.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.