Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error near unexpected token `(' in node js

Tags:

node.js

I started setting up a node app but it prompt me this

 syntax error near unexpected token `('
'/app.js: line 1: `var http = require("http");

this is my code for app.js

var http = require("http");
http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

can anyone help me in this

like image 414
customio Avatar asked Jul 29 '26 23:07

customio


1 Answers

This is probably because you are running your script as if it is a bash script. You have two options

  1. run your program with the command $ node your_pgm.js
  2. add the line #!/path/to/node to the beginning of your program (it must be the very first line)
like image 75
Soulimane Mammar Avatar answered Jul 31 '26 14:07

Soulimane Mammar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!