最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node js returning Syntax error: Unexpected identifier - Stack Overflow

programmeradmin2浏览0评论

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

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?

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?

Share Improve this question edited Nov 15, 2016 at 10:23 jwpfox 5,23211 gold badges48 silver badges42 bronze badges asked Nov 14, 2016 at 9:18 antobboantobbo 2851 gold badge6 silver badges22 bronze badges 3
  • 1 Please share code that you have tried so we can figure out error easily – Gopal Joshi Commented 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 Commented 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 – Rahul Arora Commented Nov 14, 2016 at 9:26
Add a comment  | 

8 Answers 8

Reset to default 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)

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:

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

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

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

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.

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 .

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.

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

发布评论

评论列表(0)

  1. 暂无评论