Node: Unexpected end of input

Created on 1 Dec 2015  路  5Comments  路  Source: nodejs/node

Hi :)
Yesterday, I tried a new script file on my server. I got an error because I forgot to declare a variable. I correct it, but since that, I always get this error and I don't understand why:

undefined:1

SyntaxError: Unexpected end of input
    at Object.parse (native)
    at Object.<anonymous> (/home/gregduc/botssql.js:19:39)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

Can anyone can help me?

My scipt use mysql, fs, node steam modules.
My code:

// User information
var FileSystem = require('fs');
var mysql      = require('mysql');
var SteamInterface = require('steam');
var connection = mysql.createConnection({
  host     : 'xxx',
  user     : 'xxx',
  password : 'xxx',
  database : 'xxx'
});

connection.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

  var ACCOUNT_USERNAME = ''; // Fill these in for your account.
  var ACCOUNT_PASSWORD = '';
  var AUTH_CODE = '';
  var idb = x;
  var GAMES_TO_IDLE =[];

  var online=''


connection.query('SELECT * FROM bots WHERE idb=' + idb,function(err,rows){
  if(err) throw err;
  ACCOUNT_USERNAME = (rows)[0].username; // Fill these in for your account.
  console.log('Utilisateur: '+ ACCOUNT_USERNAME)
  ACCOUNT_PASSWORD = (rows)[0].password;
  AUTH_CODE = (rows)[0].auth_code;
  var tps=((rows)[0].games).split(",").map(Number)
  console.log("Jeux jou茅s:" + tps)

for (i = 0; i < tps.length; i++) {
    var object={}
    object.game_id=tps[i]
    GAMES_TO_IDLE.push(object);
}
  online=(rows)[0].online
});



// // connection.end(function(err) {
  // // // The connection is terminated gracefully
  // // // Ensures all previously enqueued queries are still
  // // // before sending a COM_QUIT packet to the MySQL server.
// // });


// Use indexed servers if applicable
if(FileSystem.existsSync('servers.json')){
        SteamInterface.servers = JSON.parse(FileSystem.readFileSync('servers.json'));}

// Initialize the virtual client
var iSteamClient = new SteamInterface.SteamClient();
var iSteamUser = new SteamInterface.SteamUser(iSteamClient);
var iSteamFriends = new SteamInterface.SteamFriends(iSteamClient);




// Connect to Steam and attempt to log on
iSteamClient.connect();
iSteamClient.on('connected', function(){
        if(FileSystem.existsSync(ACCOUNT_USERNAME+'.dat')){
                var SentryData = FileSystem.readFileSync(ACCOUNT_USERNAME +'.dat');
                iSteamUser.logOn({
                        account_name: ACCOUNT_USERNAME,
                        password: ACCOUNT_PASSWORD,
                        sha_sentryfile: SentryData
                });
        }else{
                if(AUTH_CODE.length > 0){
                        iSteamUser.logOn({
                                account_name: ACCOUNT_USERNAME,
                                password: ACCOUNT_PASSWORD,
                                auth_code: AUTH_CODE
                        });
                }else{
                        iSteamUser.logOn({
                                account_name: ACCOUNT_USERNAME,
                                password: ACCOUNT_PASSWORD
                        });
                }
        }

});

// Retrieve log in response
iSteamClient.on('logOnResponse', function(response){
        if(response.eresult == SteamInterface.EResult.OK){
            console.log('[Steam] Successfully logged in.');

                // Set user to display Online
            if(online==1){
            iSteamFriends.setPersonaState(SteamInterface.EPersonaState.Online);
            }
                // Start idling target applications
            iSteamUser.gamesPlayed(GAMES_TO_IDLE);
            console.log('[Steam] Logging game hours!');

        }

        else{
                iSteamClient.disconnect();
                if(response.eresult == SteamInterface.EResult.InvalidLoginAuthCode){
                        console.log('[Steam] Auth Code expired. Run again with no auth code for new Auth Code email.');
                }else if(response.eresult == SteamInterface.EResult.AccountLogonDenied){
                        console.log('[Steam] Auth Code generated. Check your email and edit the script.');
                }else{
                        console.log('[Steam] Login error, ID: ' + response.eresult.toString());
                }
        }
});

// Fetch and save Sentry verification
// iSteamUser.on('updateMachineAuth', function(SentryData, callback){
        // console.log('[Steam] Retrieved sentry information.');
        // FileSystem.writeFileSync(ACCOUNT_USERNAME+'.dat', response.bytes, function(err){
                // if(err){
                        // console.log('[Steam] Failed to write Sentry file.');
                        // console.log(err);
                // }else{
                        // console.log('[Steam] Sentry file written.');
                // }
        // });
        // callback({ sha_file: MakeSha(response.bytes) });
// });

// Fetch and index servers
iSteamClient.on('servers', function(servers){
        FileSystem.writeFile('servers.json', JSON.stringify(servers));
});

// Provide some error handling
function onErr(err){
        console.log(err);
        return 1;
}

Thank you for your help :)

question

All 5 comments

fs.readFileSync() returns a Buffer by default. JSON.parse() cannot parse a Buffer. You need to pass JSON.parse() a string:

SteamInterface.servers = JSON.parse(FileSystem.readFileSync('servers.json', { encoding: 'utf8' }));

I have the same error after replacing the line code...

SyntaxError: Unexpected end of input
    at Object.parse (native)
    at Object.<anonymous> (/home/gregduc/botgm.js:57:39)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3
// User information
var FileSystem = require('fs');
var mysql      = require('mysql');
var SteamInterface = require('steam');
var connection = mysql.createConnection({
  host     : 'x',
  user     : 'x',
  password : 'x',
  database : 'x'
});

connection.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

  var ACCOUNT_USERNAME = ''; // Fill these in for your account.
  var ACCOUNT_PASSWORD = '';
  var AUTH_CODE = '';
  var idb = x;
  var GAMES_TO_IDLE =[];

  var online=''


connection.query('SELECT * FROM bots WHERE' + idb,function(err,rows){
  if(err) throw err;
  ACCOUNT_USERNAME = (rows)[0].username; // Fill these in for your account.
  console.log('Utilisateur: '+ ACCOUNT_USERNAME)
  ACCOUNT_PASSWORD = (rows)[0].password;
  AUTH_CODE = (rows)[0].auth_code;
  var tps=((rows)[0].games).split(",").map(Number)
  console.log("Jeux jou茅s:" + tps)

for (i = 0; i < tps.length; i++) {
    var object={}
    object.game_id=tps[i]
    GAMES_TO_IDLE.push(object);
}
  online=(rows)[0].online
});



// // connection.end(function(err) {
  // // // The connection is terminated gracefully
  // // // Ensures all previously enqueued queries are still
  // // // before sending a COM_QUIT packet to the MySQL server.
// // });


// Use indexed servers if applicable
if(FileSystem.existsSync('servers.json')){
        SteamInterface.servers = JSON.parse(FileSystem.readFileSync('servers.json', { encoding: 'utf8' }))};

// Initialize the virtual client
var iSteamClient = new SteamInterface.SteamClient();
var iSteamUser = new SteamInterface.SteamUser(iSteamClient);
var iSteamFriends = new SteamInterface.SteamFriends(iSteamClient);




// Connect to Steam and attempt to log on
iSteamClient.connect();
iSteamClient.on('connected', function(){
        if(FileSystem.existsSync(idb+'.dat')){
                var SentryData = FileSystem.readFileSync(idb +'.dat');
                iSteamUser.logOn({
                        account_name: ACCOUNT_USERNAME,
                        password: ACCOUNT_PASSWORD,
                        sha_sentryfile: SentryData
                });
        }else{
                if(AUTH_CODE.length > 0){
                        iSteamUser.logOn({
                                account_name: ACCOUNT_USERNAME,
                                password: ACCOUNT_PASSWORD,
                                auth_code: AUTH_CODE
                        });
                }else{
                        iSteamUser.logOn({
                                account_name: ACCOUNT_USERNAME,
                                password: ACCOUNT_PASSWORD
                        });
                }
        }

});

// Retrieve log in response
iSteamClient.on('logOnResponse', function(response){
        if(response.eresult == SteamInterface.EResult.OK){
            console.log('[Steam] Successfully logged in.');

                // Set user to display Online
            if(online==1){
            iSteamFriends.setPersonaState(SteamInterface.EPersonaState.Online);
            }
                // Start idling target applications
            iSteamUser.gamesPlayed(GAMES_TO_IDLE);
            console.log('[Steam] Logging game hours!');

        }

        else{
                iSteamClient.disconnect();
                if(response.eresult == SteamInterface.EResult.InvalidLoginAuthCode){
                        console.log('[Steam] Auth Code expired. Run again with no auth code for new Auth Code email.');
                }else if(response.eresult == SteamInterface.EResult.AccountLogonDenied){
                        console.log('[Steam] Auth Code generated. Check your email and edit the script.');
                }else{
                        console.log('[Steam] Login error, ID: ' + response.eresult.toString());
                }
        }
});

// Fetch and save Sentry verification
iSteamClient.on('sentry', function(SentryData){
        console.log('[Steam] Retrieved sentry information.');
        FileSystem.writeFile(idb, SentryData, function(err){
                if(err){
                        console.log('[Steam] Failed to write Sentry file.');
                        console.log(err);
                }else{
                        console.log('[Steam] Sentry file written.');
                }
        });
});

// Fetch and index servers
iSteamClient.on('servers', function(servers){
        FileSystem.writeFile('servers.json', JSON.stringify(servers));
});

// Provide some error handling
function onErr(err){
        console.log(err);
        return 1;
}

I don't understand why it was working before and not now...

SOLVED.

How did ya solve it? I'm seeing this now after switching to strict mode also. Mine are thrown on the first line.

@tomachinz you should consider asking on https://github.com/nodejs/help/issues instead

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cong88 picture cong88  路  3Comments

Brekmister picture Brekmister  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

addaleax picture addaleax  路  3Comments

willnwhite picture willnwhite  路  3Comments