SyntaxError: Unexpected identifier
Asked Answered
C

3

6

I am new to node.js. I created a file named app.js and put this code in that file using express to switch the template engine:

//module dependencies

var express = require('express');
    routes = require ('./routes');
    user = require ('./routes/user');
    http= require ('http');
    path = require ('path');

var exphbs = require ('express3-handlebars');
var app = express();

//all environement
app.set ('port', process.env.PORT || 3000);
app.set('views', __dirname +'/views');
//app.set('view engine','jade');
app.engine('handlebars',exphbs({defaultLayout :'main'}));
app.set('view engine ','handlebars');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname,'public')));

//developpement only
if ('developpement' == app.get('env')){
    app.use(express.errorHandler());
}

//app.get('/', routes.index);
//app.get ('/user' , user.list);
app.get('/' , function(req,res) {
    res.render('home');
}
http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

Then I run the application and get this error:

http.createServer(app).listen(app.get('port'), function(){
^^^^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

This line is causing the error:

http.createServer(app).listen(app.get('port'), function(){
Chaudoin answered 10/2, 2015 at 23:0 Comment(5)
missing ). plz close.Poetize
One line above the error.Poetize
app.get('/' , function(req,res) { res.render('home'); } should be app.get('/' , function(req,res) { res.render('home'); });Caffrey
what about this : module.js:340 throw err; ^ Error: Cannot find module 'C:\nodeapps\test\node_modules\app.js' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:929:3Lexicon
By the way, you are leaking variables into global scope like a broken toilet... either put var in front of every variable declaration or use colons instead of semicolons.Oruro
O
10

You are missing a closing brace in

app.get('/' , function(req,res) {
  res.render('home');
}) // <-- the last one

You should use an editor that provides proper syntax highlighting and a code linter - like jshint which would warn you about this and also warn you about improper variable declarations:

var onevar = 'value'; // <-- superbad! You just ended this statement!
    another = 'val2'; // <-- now this variable leaked into global scope!
// Proper:
var onevar = 'value';
var another = 'val2';
// Also proper:
var onevar = 'value',
    another = 'val2';

The SyntaxError: Unexpected identifier is always a typo (or you trying to do something JavaScript does not understand) somewhere in your code and usually happens before the unexpected identifier. Oversimplified, it basically means that the parser was in the middle of some statement and, according to the grammar rules, the thing that followed was not acceptable for that particular situation.

Oruro answered 10/2, 2015 at 23:38 Comment(3)
ok i correct the code but i got this error : Error: Cannot find module './routes' at Function.Module._resolveFilename (module.js:338:15 at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.<anonymous> (C:\nodeapps\test\app.js:4:14) at Module._compile (module.js:456:26)Lexicon
The file must be in the same directory as the one from which you require it. It seems to be not...Oruro
Does it have .js extension? This should not be necessary, but try to include the extension in the require call.Oruro
A
3

I think this incorrect:

var express = require('express');
    routes = require ('./routes');
    user = require ('./routes/user');
    http= require ('http');
    path = require ('path');

I think you must try something like that:

var express = require('express'),
    routes = require ('./routes'),
    user = require ('./routes/user'),
    http= require ('http'),
    path = require ('path');

Or

var express = require('express');
var routes = require ('./routes');
var user = require ('./routes/user');
var http= require ('http');
var path = require ('path');
Ailbert answered 10/2, 2015 at 23:23 Comment(2)
what about this error dave : Error: Cannot find module './routes' at Function.Module._resolveFilename (module.js:338:15 at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.<anonymous> (C:\nodeapps\test\app.js:4:14) at Module._compile (module.js:456:26)Lexicon
@NafaLamine what is in the . ('./routes') for your machine? The path doesn't exist. It would seem.Seale
W
0

I noticed that if there's no line brake after an argument, we need to add a ;.

For example:

This will run without any error message:

let num1 = 0;
let num2 = 5;
while(num1 < num2){
console.log(num1) //notice that there's no ";"
num1++;
}

And if the same, in one line:

let num1 = 0; let num2 = 5; while(num1 < num2){ console.log(num1) num1++;}

it will give you error.

To prevent this, just add a ; where needed, like so:

let num1 = 0; let num2 = 5; while(num1 < num2){ console.log(num1); num1++;}
Whelp answered 22/8 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.