Run a script file with Node
Create a folder and place the following index.js
file in it
// index.js file
console.log("Hello Node!");
Open the terminal at the folder containing the index.js
. In the terminal, run the following command:
node index.js
This must produce the following output:
Hello Node!
Since node can only execute .js
files, adding .js
extension to the file path is optional! So, you can also use the command node index
in the terminal to run the index.js
file.
If we provide a directory path instead of a file path to Node, it will try to find and run an index.js
file inside that directory. So, in our case, we can also run the index.js
file by simply typing node .
in the terminal (notice the proceeding dot that indicates "current folder").
If you just enter node
in the terminal then you will run Node's repl
module which provides a Read-Eval-Print-Loop (REPL) implementation.