J64 J64 - 18 days ago 4
Javascript Question

Node server won't start, gives unexpected token

Every time I try to start my server it gives me a long error:

SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ubuntu/workspace/isal/routes/index.js:5:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)


The file with the syntax error is this index.js file It claims that there is a parsing error, unexpected token with the final line, but has nothing after unexpected token and I don't really see the problem with the module exporting.

var express = require('express');
var router = express.Router();
var imgur = require('../services/imgur');

router.get('/', function(req, res) {
res.send('Hello');
});

router.get('/latest', function(req, res) {

});

router.get('/search/:q', function(req, res) {
imgur.getImage(req.params.q, req.query.offset).then(ans => {
res.json(ans);
});

module.exports = router;

Answer

you are not properly closing your final function call:

var express = require('express');
var router = express.Router();
var imgur = require('../services/imgur');

router.get('/', function(req, res) {
  res.send('Hello'); 
});

router.get('/latest', function(req, res) {

});

router.get('/search/:q', function(req, res) {
    imgur.getImage(req.params.q, req.query.offset).then(ans => {
        res.json(ans);
    }); <---------------- Missing
});

module.exports = router;

When you get a syntax error like this, it basically means start from where the error occurred, and backtrack up in your file until you find the syntax error. There will always be a syntax error, if a syntax error is thrown. You should be using a tool to code with syntax highlighting and save yourself a world of problems, like an IDE.