Node.js syntax error "unexpected token" on "exports."

Saumya Tripathi picture Saumya Tripathi · Jan 12, 2013 · Viewed 64.5k times · Source

I have been trying to learn node.js. I am trying to create a simple node.js web api and a html-javascript front end to login using Facebook auth and store Facebook id in Mongodb.

I was able to do it by following tutorials available online.

Now I want to segregate the code into multiple files but when I try to create a route "user" and expose functions via exports. I am getting the following error.

module.exports.userLogin = function(req,res){
      ^ SyntaxError: Unexpected token .
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\Users\Saumya\Desktop\vhsharedraft\web.js:2:6)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

EDIT#1

module.exports.userLogin = function(req,res){
    graph.setAccessToken(req.session.fb.access_token);

    graph.get("/me", function(err, data) {
        if(err){
            console.log('Error obtaining data.');
            return;
        }
        console.log(data);
    }

}

EDIT # 2

var mongo = require('mongodb'),
graph = require('fbgraph');



exports.userLogin = function(req,res){
    graph.setAccessToken(req.session.fb.access_token);

    graph.get("/me", function(err, data) {
        if(err){
            console.log('Error obtaining data.');
            return;
        }
        console.log(data);
    }
}

This is all I have in user route. Actually, I was making a real silly mistake, I left a comma in front of graph = require('fbgraph') instead of semi colon. After fixing this syntax error, I am getting this error.

}
^
SyntaxError: Unexpected token }
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\Users\Saumya\Desktop\vhsharedraft\web.js:2:6)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

Answer

Jamund Ferguson picture Jamund Ferguson · Jan 12, 2013

You have a typo here:

graph.get("/me", function(err, data) {
    if(err){
        console.log('Error obtaining data.');
        return;
    }
    console.log(data);
}

It should be:

graph.get("/me", function(err, data) {
    if(err){
        console.log('Error obtaining data.');
        return;
    }
    console.log(data);
}); /* Note the added parenthesis here */

Also use exports.userLogin instead of module.exports.userLogin