3

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?

3
  • 1
    Please share code that you have tried so we can figure out error easily Nov 14, 2016 at 9:21
  • thanks, I don't have much just one js file called HelloWorld.js with this line in it console.log("Hello World");at the moment, nothing else. The file sits in the Desktop directory. This is what's in the console at the moment: > node HelloWorld.js node HelloWorld.js ^^^^^^^^^^ SyntaxError: Unexpected identifier >
    – antobbo
    Nov 14, 2016 at 9:24
  • 1
    try typing node and once the console opens just type require("./HelloWorld.js"). See if you get the error again Nov 14, 2016 at 9:26

8 Answers 8

9

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)

8
  • Yes I was in the console already, but I accessed it by clicking on the nodeje icon application. I closed that, opened the normal windows console, cd'd to the node directory C:\Program Files\nodejs>node and then typed node HelloWorld.js from there, but I still got the same result, the syntax error. I've CTRL + C to come out of nodejs (but still in the window console) and run node HelloWorld.js from there, expecting to get a different error (in fact I got Error: Cannot find module 'C:\Program Files\nodejs\HelloWorld.js')
    – antobbo
    Nov 14, 2016 at 9:37
  • No you need to cd to your file directory and not the place where node is installed Nov 14, 2016 at 9:39
  • sorry to add to it, no the script file as mentioned is on the desktop while the nodejs application is in C:\Program Files\nodejs\. Do I have to move my HelloWorld.js to the nodejs directory?
    – antobbo
    Nov 14, 2016 at 9:39
  • No you do not need to move your file. Just cd to your file directory. For example if your file is in 'hello' folder on the desktop, you simply need to cd Desktop/hello and then do node HelloWorld.js Nov 14, 2016 at 9:41
  • OK, I now cd'd to my desktop where the HelloWorld.js file sits but if I type 'node' in the prompt I get another error: C:\Users\antoniob\Desktop>node 'node' is not recognized as an internal or external command, operable program or batch file. C:\Users\antoniob\Desktop>
    – antobbo
    Nov 14, 2016 at 9:41
3

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

2

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).

0

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

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.

0

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 .
0

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.

1
  • To clarify: I used another tutorial in Dutch on kinsta.com/nl/kennisbank/wat-is-node-js. Use the Javascript code in there and then in your web browser type: "http: // localhost:3000". There you will see the Hello world output
    – C-Bass
    May 18, 2022 at 14:10
-1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.