Running a JavaScript File
Running a JavaScript file means Node.js reads the file and runs its lines. A file is a named set of lines stored on a computer.
The run command
This command runs the file named hello.js. Run it in a terminal.
node hello.jsThe file
This line sits inside the file hello.js.
console.log("Hello, Node!");Example
console.log("Hello, Node!");
console.log("Node runs this file.");Key points
- Save JavaScript in a file with a .js ending.
- The ending .js marks the file as JavaScript.
- Type node hello.js to run a file named hello.js.
- hello.js is the file name in this lesson.
- Node.js reads the file from the top line down.
- The terminal shows the output after the run.
In this example, the file hello.js holds two lines. The first line prints Hello, Node!. The second line prints Node runs this file. The command node hello.js runs both lines.
Note
Run the command from the folder that holds the file.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command runs a file named hello.js?