Execute a JS file using node file.js for server scripts, <script src="file.js"> for web pages, or cscript file.js on Windows.
A double-click on a .js file usually opens it in a text editor or does nothing at all. That is because JavaScript is an interpreted language that needs a specific runtime environment to execute. The exact command you use depends entirely on where the code is meant to run—on a server, in a web browser, or as a legacy Windows automation script. This guide gives you the working command for each environment.
Running A JS File In Node.js (The Standard Method)
Node.js is the cross-platform runtime used to execute JavaScript outside of a browser. It handles everything from utility scripts to full server backends. Before running a .js file this way, Node.js must be installed on your machine.
Save your JavaScript code in a file ending with .js, such as app.js. Open a terminal, navigate to the directory containing the file using cd, and run the command node app.js. Node executes the file line by line and prints any output to the terminal.
Node offers several handy flags for different situations:
node -e "console.log('hello')"evaluates a string of code directly without needing a file.node --watch app.jsautomatically restarts the script whenever the file changes—useful during development.node --run testruns a named script from yourpackage.jsonfile.
If the node command is not recognized, the runtime is either not installed or not added to your system PATH. Download the latest LTS version from the official Node.js website to fix this.
Executing A JS File In The Browser (Console & HTML)
Browsers execute JavaScript to power interactive web pages. There are two ways to run a .js file here.
The quickest method is the DevTools Console. Open it with Control+Shift+J on Windows and Linux, or Command+Option+J on Mac. You can paste JavaScript directly into the console and press Enter to run it. This is useful for testing small snippets or debugging live pages.
The second method is to attach the file to an HTML page using a <script> tag. Place <script src="./app.js"></script> just before the closing </body> tag. When the page loads in the browser, the JavaScript file executes automatically.
Note that browser code has access to the document and window objects, while Node.js scripts do not. Trying to use browser-specific APIs in a Node environment will throw a reference error, and vice versa.
Running A JS File On Windows (WSH For Legacy Scripts)
Windows includes a legacy feature called Windows Script Host that can run .js files directly from the command line. This is primarily used for older Windows automation tasks and is not intended for modern cross-platform development.
Open a Command Prompt and use the command cscript file.js. The output appears in the terminal window. If double-clicking the file does nothing, WSH may be disabled in the system settings. Using the cscript command explicitly bypasses that issue and runs the script cleanly.
Which Runtime Should You Use For Your JS File?
The right runtime depends entirely on what you want the script to do. The table below compares the common options.
| Environment | Best For | Execution Command |
|---|---|---|
| Node.js | Server scripts, utilities | node app.js |
| Browser Console | Quick testing, debugging | Ctrl+Shift+J |
| HTML Script Tag | Web pages | <script src="app.js"> |
| Windows WSH | Windows automation (legacy) | cscript app.js |
| Node REPL | One-liner experiments | node (then type code) |
| Node Watch Mode | Active development | node --watch app.js |
| Package.json Scripts | Project task runners | npm run start |
Why Your JS File Isn’t Running (Common Fixes)
Most execution problems come down to a missing runtime or a misunderstanding about which environment the code expects. This table covers the common failures and their solutions.
| Mistake | Why It Happens | The Fix |
|---|---|---|
| File opens in Notepad | OS associates .js with a text editor |
Use node file.js in the terminal |
node is not recognized |
Node.js is not installed | Install the LTS version from the Node.js website |
document is not defined |
Running browser code in Node | >Use the browser console instead |
| Script runs but shows nothing | No output command in the code | Add a console.log() statement |
| Double-click does nothing | WSH is missing or disabled | Run cscript file.js from CMD |
| File not found error | Terminal is in the wrong directory | Use cd to enter the file’s folder |
Final Execution Guide
Pick your runtime based on the task at hand. For any modern server-side or local utility script, install Node.js and run node yourfile.js. For web interactivity, load the file with a <script> tag in your HTML. For older Windows automation, use cscript in a Command Prompt. Matching the file to the right command makes execution instant.
References & Sources
- Node.js. “Run Node.js scripts from the command line.” Official documentation covering
node,--watch, and REPL commands. - Chrome DevTools. “Run JavaScript in the Console.” Official reference for the browser console and keyboard shortcuts.
