antobbo
antobbo

Reputation: 285

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?

Upvotes: 4

Views: 44730

Answers (8)

Craig D.
Craig D.

Reputation: 41

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

Upvotes: 3

user19230626
user19230626

Reputation: 11

On windows hit CTRL + D to exit REPL and then run HelloWorld.js again

Upvotes: 0

C-Bass
C-Bass

Reputation: 1

Although the question is old, I just solved it. So for anyone who still likes an answer: Apparently Node.Js installs two different consoles or executables. There is "Node.js" and there is "Node.js command prompt". Use the latter and it will work

To clarify, I used another tutorial in Dutch. Use the Javascript code in there and then in your web browser type http://localhost:3000. There you will see the Hello World output.

Upvotes: 0

O'talb
O'talb

Reputation: 131

I'm on linux and I'd the same issue what I was writing in terminal is :

  1. node
  2. node file.js all what I had to do is to write node file.js from the start without writing node first .

Upvotes: 0

If people are facing below-mentioned error: Uncaught SyntaxError: Unexpected identifier at the time of running, console.log("Hello World"); code with command, node HelloWorld.js, in VS code editor

Problem :

node HelloWorld.js ^^^^^ Uncaught SyntaxError: Unexpected identifier

Solution :

(1) Just install Babel JavaScript extension in VS code editor

(2) After Babel JavaScript extension is installed, save your Program and then run your program with the command, node HelloWorld.js

Definitely will get the expected result.

Upvotes: 0

Gullible
Gullible

Reputation: 1

I had the same issue when following an online course, my mistake was that i did not safe the file i was following as .js in the name when saving. Therefore my Hello.js did not open because it was only Hello

Upvotes: 0

Quentin
Quentin

Reputation: 944256

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

You should type JavaScript into the Node.js console.

node is a program name. HelloWorld.js is a command line argument. They are not JavaScript.

You should type those into your shell (e.g. Windows Powershell or bash).

Upvotes: 2

Rahul Arora
Rahul Arora

Reputation: 4543

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)

Upvotes: 9

Related Questions