尝试运行“节点”时出现SyntaxError。我如何解决它?

我刚刚开始学习,这是我尝试启动机器人时遇到的错误。 关于如何解决此问题的任何想法?

SyntaxError是:

    SyntaxError: C:\Users\Thela\code\config.json: Unexpected end of JSON input
        at JSON.parse (<anonymous>)
        at Object.Module._extensions..json (internal/modules/cjs/loader.js:987:27)
        at Module.load (internal/modules/cjs/loader.js:812:32)
        at Function.Module._load (internal/modules/cjs/loader.js:724:14)
        at Module.require (internal/modules/cjs/loader.js:849:19)
        at require (internal/modules/cjs/helpers.js:74:18)
        at Object.<anonymous> (C:\Users\Thela\code\index.js:2:16)
        at Module._compile (internal/modules/cjs/loader.js:956:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
        at Module.load (internal/modules/cjs/loader.js:812:32)

index.js

    const Discord = require('discord.js');
    const config = require('./config.json');
    const client = new Discord.Client();

    client.once('ready',() => {
        console.log('Ready!');
    });

    client.on('message',message => {
        if (message.content === '!ping') {
            message.channel.send('Pong.');
        }
    });

    client.login(config.token);
    enter code here

并且config.json

    {
        "prefix": "!","token": "<my-token>",}
shikabulu 回答:尝试运行“节点”时出现SyntaxError。我如何解决它?

config.json应该像这样:

{
    "prefix": "!","token": "<my-token>"
}

第3行上的“,”不应在此处。

,

与Javascript不同,JSON中不允许尾随逗号。因此,将配置更改为此:

{
    "prefix": "!","token": "<my-token>"
}
,

问题不在于代码中,而在于JSON配置文件中。

JSON格式不允许尾随逗号,因此在您的情况下,只需将其删除即可轻松解决。您的新配置文件应如下所示:

{
    "prefix": "!","token": "<my-token>"
}

此外,我注意到您的代码中包含enter code here,不确定是否要删除它或将其作为注释,但是在您的代码中保留此原样肯定会导致一些错误

本文链接:https://www.f2er.com/3159608.html

大家都在问