Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node js returning Syntax error: Unexpected identifier

I downloaded and installed node.js on Windows and I'm following a simple tutorial from nodebeginner.org.

I've created a file called HelloWorld.js which contains just:

console.log("Hello World");

When I type node HelloWorld.js in the node.js console I get:

SyntaxError: Unexpected identifier

I checked my classpath variable and it has the C:\Program Files\nodejs\ on it.

HelloWorld.js is still open in Notepad++ for editing.

What am I doing wrong?

like image 517
antobbo Avatar asked Oct 19 '25 09:10

antobbo


2 Answers

I think you are already in the the console.

Just follow the steps to fix the error:

1) Try doing CTRL + C couple of times. See if you exit the console

2) Then do node HelloWorld.js

I think you will get your output

When in your node console already, you can simply do require("./HelloWorld.js") to get the output. (Given that you are in the directory that contains this file)

like image 88
Rahul Arora Avatar answered Oct 22 '25 00:10

Rahul Arora


A little late but I figured this out as I'm learning it as well. You are not in the correct Node.js command window:

enter image description here

You are probably trying to run Node.js, ie. the one with the red arrow. This gives you the "Unexpected identifier" error. You want the Node.js command prompt, or the one shown with a green arrow.

Craig

like image 24
Craig D. Avatar answered Oct 21 '25 22:10

Craig D.



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!