6

I have a javascript file hello.js with console.log("Hello World"). I want to run it from my terminal (I am on a mac). I have node installed and I take the following steps -

  1. Open terminal and navigate to the directory where hello.js exists.
  2. Run command "node hello.js"

But I dont see the console statement (Hello World) in my terminal.

Can some one please help me run javascript from terminal (or tell me what I am doing wrong) ?

PS: I have checked this thread but my problem still exists.

pwolaq
  • 6,343
  • 19
  • 45
Shiv Baral
  • 409
  • 4
  • 11
  • 18

4 Answers4

2

One possible error is that your JavaScript file doesn't end with a new line character.

The node parser will read the last line, but it won't completely process the line unless it includes a new line marker (or, possibly, a semicolon).

Make sure your file includes the new line (as well as, preferably, a semicolon), i.e.:

console.log("Hello World");
// EOF comment, to validate a new line marker after last line of code.

This might solve your issue - unless the reason for your issue lies somewhere else.

Good luck!

Myst
  • 18,516
  • 2
  • 45
  • 67
0

You have to first accurately create the path to the .js file. This is probably your problem.

When you first open the CLI (Command Line) there will be a path displayed. For example...

C:\Users\yourname>

from there you have to get to where the file is located, so you type in.. C:\Users\yourname>cd\Users\yourname\Documents\course\jsfolder

cd means "change directory"..and just put the full path to where the file is located.

Now to see if you are there by typing dir on the end

C:\Users\yourname>\Documents\course\jsfolder>dir

This will list all the files in that last directory. You should see your New.js file in there. You are now ready to go!

Follow what Anupam said above...type.. node New.js ..and your file will run if you have node.js installed on your computer..BINGO!>

one last thing ..I believe the command "node" is "$node" on an Apple

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – mohammad mobasher Feb 03 '22 at 07:12
-1

There are a few additional solutions you could use other than node.

The expected behavior of node would be to print "Hello World" for you, and there has been help troubleshooting in the comments of your post.

jsc is included with macOS. You should make a link to the file first as described in this post, and change your console.log() methods to debug().

rhino is available from Mozilla. It would require you to switch your console.log() methods to print().

For installing rhino, you may be able to avoid some of the issues you've had with node by installing through homebrew. As a last means of troubleshooting node, you may also find it useful to reinstall it through homebrew.

Ehler
  • 1
  • 2
-1

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName.js”.

  1. Open Terminal or Command Prompt.
  2. Set Path to where File is Located (using cd).
  3. Type “node New.js” and Click Enter
Anupam Rai
  • 19
  • 4