How To Run JavaScript File From Command Line Using NodeJS
This is a simple tutorial about running JavaScript in NodeJS environment.
First, this requires installing NodeJS on your system.
So make sure you've done that first.
To check if you already have NodeJS installed, run this command from Terminal:
node --version
If you see a brief message showing Node version, it's already installed on your computer.
If you see "NodeJS is not found" error, you need to install it, and if it's already installed, you need to also add NodeJS installation directory (C:\nodejs for example,) to PATH variable under Environment Variables on Windows.
How to run JavaScript in Terminal on Command Line:
Navigate to where your JavaScript project is located on your hard drive.
This can be done by using the cd (change directory) command on either Mac or Windows:
cd C:\my_projects\js_project
(To create a new JS projects just create a new folder and place a JavaScript file in it.)
Let's say you also created a file called index.js and placed it into your js_project folder.
Then you can use NodeJS in Terminal or command line to execute a single JavaScript file:
node filename.js
So this is how you run JavaScript file directly from your command line or Terminal using node command. Before executing this command, just make sure to navigate to your JavaScript's project directory first, using cd (change directory.)