How To Execute Node.js File | The node Command

Execute a Node.js file by opening a terminal in the file’s directory and running the node filename.js command.

You’ve written a JavaScript file. Now you need to run it outside the browser. The command node app.js is the standard bridge between your code and the server-side runtime, and getting it right takes seconds once you know the workflow. This article covers the exact steps, common pitfalls like PATH errors, and advanced options like watch mode.

How To Execute A Node.js File: The Basic Command

The standard method to execute a Node.js file is to open a terminal, navigate to the directory containing the file, and type node filename.js. For this to work, Node.js must be installed and the node command must be available in your system PATH. You can verify the installation by running node -v to check your version number.

Step-by-Step: Running Your First .js File

Follow these three steps to run your first Node.js script.

  1. Create a file. Make a new file called myfirst.js and add a simple test line: console.log("Hello World");
  2. Open a terminal in the same folder. On Windows, use Command Prompt, PowerShell, or the integrated terminal in VS Code. On macOS or Linux, open the Terminal app. Use the cd command to change into the directory containing your file.
  3. Run the command. Type node myfirst.js and press enter. The printed result will be Hello World, confirming the file executed successfully.

If you see Hello World in your terminal, Node.js is working correctly and you have just executed your first server-side script.

Common Mistakes When Running Node.js Files

Beginners often encounter a few predictable errors when running Node.js files. The most frequent mistake is running the command from the wrong directory. Always check that your terminal session is pointed to the exact folder holding your script. A second common error is typing the wrong file name or forgetting the .js extension. While node app works in some cases, node app.js is the standard approach. If you get the error 'node' is not recognized, Node.js is either not installed or not added to your PATH. Restarting your terminal or your computer after installation usually resolves this.

Command Description Best Used For
node app.js Standard execution of a single file Running any simple script
node -e "console.log(123)" Evaluate inline JavaScript code Quick tests and math checks
node --watch app.js Auto-restart the script on file changes Development servers and debugging
node -v Print the installed Node.js version Verify installation and version requirements
node -p "2+2" Evaluate and print the result of inline code Simple calculations in the terminal
node --inspect app.js Start a debugging session for the script Advanced debugging with Chrome DevTools
node --experimental-modules app.mjs Run a file using ES modules Projects using import syntax

Why Can’t I Just Double-Click A .js File?

Unlike an .exe file on Windows, double-clicking a .js file on most operating systems will not run it through Node.js. The file needs the Node.js runtime explicitly invoked, which is what the node command does in the terminal. This is why the command-line method is the standard way to execute a Node.js file. For a quick test, you can drag a .js file onto the node executable in your file manager, but the terminal offers the most control and feedback.

What Is The Shebang Line And When Should You Use It?

On Unix-like systems (macOS and Linux), you can make a Node.js file directly executable by adding a shebang line. The shebang tells the operating system to use the Node.js runtime to execute the file. To use it, add #!/usr/bin/env node as the first line of your .js file. Then make the file executable with chmod u+x app.js. Now you can run it directly from the terminal by typing ./app.js without explicitly typing the node command. This is the standard approach for turning Node.js scripts into reusable command-line tools.

Execution Method Best Use Case Key Command
Standard node command Running any simple script one time node script.js
Watch Mode (--watch) Development and debugging node --watch script.js
Shebang executable (#!/usr/bin/env node) Turning scripts into command-line tools ./script.js after chmod u+x
Inline evaluation (-e) Testing small code snippets node -e "console.log(1)"

Quick-Start Sequence To Execute A Node.js File

Here is the final checklist to execute a Node.js file correctly every time.

  1. Create a .js file containing your JavaScript code.
  2. Open a terminal in the directory where the file is stored.
  3. Run the command node filename.js.
  4. Check the terminal output to confirm the script ran as expected.

If the file does not run, verify that Node.js is installed and that the terminal path points to the correct folder. The Node.js official documentation for command-line execution provides further details on advanced options and troubleshooting.

References & Sources