Run Javascript file from command line

13,677

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!

Share:
13,677
Shiv Baral
Author by

Shiv Baral

Updated on July 23, 2022

Comments

  • Shiv Baral
    Shiv Baral almost 2 years

    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.

  • Homungus
    Homungus almost 4 years
    this is not helping, this is exactly what the thread-opener already tried.
  • mohammad mobasher
    mohammad mobasher about 2 years
    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.