5

I am new to node.js I just finished installing it on my windows machine. Actually i'm following a tutorial on tutorialspoint. After the installation I was told to create a main.js file and put the following code in the file.

/* Hello, World! program in node.js */
console.log("Hello, World!")

I executed main.js file using Node.js interpreter by typing $ node main.js, But I had the following errors.

SyntaxError: Unexpected identifierat Object.exports.createScript      
(vm.js:24:10)
at REPLServer.defaultEval (repl.js:221:25)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)

Please help me out. Thank you.

2

4 Answers 4

6

Sounds like you are in the REPL(Read-Eval-Print-Loop). Try to hit ctrl + c a couple of times and see if you exit out to the command prompt. THEN try running node main.js. You should see your desired output.

Sign up to request clarification or add additional context in comments.

4 Comments

I am using windows machine.
That shouldn't matter. The error you've made (I think) is that you input "node" into your prompt, hit enter, and then tried to run "node main.js". You can't do this. Just run "node main.js" from the main command prompt. Don't do the first "node"
So i run what you said but the output was empty with no error from the cmd. Any reason for that. Thank you
The only other thing I can think of is that you are not in right directory. You need to cd into the directory where your main.js file is located and then run "node main.js". It's either that or you've done something wrong in the main.js file itself.
4

I think, you are run your node main.js not from shell, but from node REPL.

You don't need to run node before.

$ cat main.js
console.log("Hello, World!")
$ node main.js
Hello, World!

Hm, you are on Windows. Then you should do something like this in your cmd.exe:

c:\...> cd c:\projects\hello
c:\...> type main.js
console.log("Hello, World!")
c:\...> node main.js
Hello, World!

Note: cat and type commands above are redundant and just for file content demonstration.


Also, when you inside nodejs REPL, you can write javascript code directly.
Just try:

> console.log('Hey');
'Hey'
undefined
> require('./main.js');
Hello, World!
undefined
> exit
Bye-bye

6 Comments

Please could you explain further. And when you said cmd.exe, is it that of windows or nodejs cmd?
I'm about standard Windows shell. I don't know how you install your nodejs on windows. I don't use Windows at all. May be you have cygwin installed?
I dont have cygwin installed though.
How you open a window where you write node main.js?
I search for cmd and run it at the windows start menu.
|
0

Don't run node before you test you program.

2 Comments

could you explain further. Thanks
when you input node and enter, you will be inside the node REPL environment. In REPL, you should write code directly. like console.log("Hello World"). If you want to test a javascript file, you should run node xx.js.
0

you can run node from anywhere using normal command prompt of windows 7 but you have to specify the js file name with exact file path.

for example : c:\users[your name]>node d:\projects\js_files\main.js

it will work if you have the set path="c:\progam files\nodejs\bin" environment variable prior to doing anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.