The simplest way: use Node.js from your command line
To run a JavaScript file, you need an interpreter — a program that reads the code and executes it. The most straightforward option is Node.js, which is free and works on Windows, Mac, and Linux. Once you install it, you can run any JavaScript file by typing a single command in your terminal or command prompt.
Node.js was originally built to run JavaScript on web servers, but it works just as well for scripts on your own computer. It handles file operations, system tasks, and anything else you might write in JavaScript outside of a web browser.
Key Takeaways
- read and install Node.js from nodejs.org, which includes npm (a package manager for JavaScript libraries).
- Save your JavaScript code in a file with a .js extension, like myfile.js.
- Open your terminal or command prompt, navigate to the folder containing your file, and type node myfile.js to run it.
- If your script needs external libraries, install them with npm install and Node.js will find them automatically.
- Browser-based JavaScript (code that runs on websites) requires a different approach — you embed it in an HTML file and open that file in a web browser.
Installing Node.js on your machine
Go to nodejs.org and read the LTS (Long Term Support) version, which is the stable choice for most people. The installer will guide you through the setup. On Windows, it adds Node to your system path automatically. On Mac and Linux, the process is similar — just follow the prompts.
When the installation finishes, open a new terminal or command prompt window and type node --version. If you see a version number, Node.js is ready to use. The installer also includes npm (Node Package Manager), which you'll use if your script needs external libraries — you can check it with npm --version.
Creating and saving your JavaScript file
Write your JavaScript code in any text editor — Notepad, VS Code, Sublime Text, or any other editor you have. The editor does not matter; what matters is the file extension. Save your file with a .js extension, like script.js or myprogram.js. Do not save it as .txt or any other format.
Here is a straightforward example to test with:
console.log("Hello, this is JavaScript running on my computer"); const number = 42; console.log("The answer is " + number);
Save this as test.js in a folder you can find easily, like your Desktop or Documents folder.
Running the file from your terminal
Open your terminal (Mac and Linux) or Command Prompt (Windows). Navigate to the folder where you saved your .js file. On Windows, you might type cd Desktop if your file is on the Desktop. On Mac or Linux, the command is the same.
Once you are in the correct folder, type node test.js (or whatever your filename is) and press Enter. Node.js will run the file, and any output from console.log() statements will appear in your terminal. If your script has errors, Node.js will show you an error message telling you what went wrong and which line caused the problem.
Using external libraries with npm
Many JavaScript projects use libraries — pre-written code that handles common tasks. If your script needs a library, you install it with npm. For example, if you want to use a library called lodash, you would type npm install lodash in your terminal while in your project folder.
npm creates a folder called node_modules and downloads the library there. Your script can then import and use it with a line like const _ = require('lodash'); at the top of your file. Node.js automatically looks in the node_modules folder, so you do not have to tell it where to find the library.
JavaScript in a web browser versus on your computer
JavaScript written for websites works differently from JavaScript you run with Node.js. Browser-based JavaScript has access to the page's HTML and CSS, can respond to mouse clicks and keyboard input, and can send data to servers. It cannot read files from your hard drive or run system commands for security reasons.
If you write JavaScript meant to run in a browser, you embed it in an HTML file using <script> tags, then open that HTML file in your browser. Node.js JavaScript, by contrast, has no access to HTML or the browser — it is pure computation and file handling. Choose the right tool for what you are building: Node.js for scripts and server-side work, browser JavaScript for interactive web pages.
Troubleshooting common problems
If you type node myfile.js and get "command not found" or "node is not recognized", Node.js is not installed or not in your system path. Reinstall Node.js and make sure to restart your terminal window after installation.
If your script runs but produces the wrong output, check for typos in variable names, missing semicolons, or mismatched parentheses. Node.js error messages usually point to the line number where the problem is. If a library is not found, make sure you ran npm install libraryname in the same folder as your script, and check that the require statement matches the library name exactly.
Frequently Asked Questions
Do I have to use Node.js to run JavaScript?
Node.js is the most common choice for running JavaScript on your computer, but other runtimes exist — Deno and Bun are newer alternatives. For most people and most projects, Node.js is the standard and has the largest ecosystem of libraries.
Can I run JavaScript without installing anything?
Yes, if your code is meant for a web browser. Write it in an HTML file with <script> tags and open the HTML file in any browser. For code that needs to run outside a browser — file operations, system tasks, server work — you need Node.js or another runtime.
What is the difference between .js and .mjs files?
A .mjs file is a JavaScript module that uses the newer ES6 import syntax. Most projects use .js with Node.js, which supports both older require() and newer import syntax. Unless you are working on a project that specifically requires .mjs, stick with .js.
Why does my script run but nothing appears on the screen?
Your script probably does not have any console.log() statements. Add console.log("test"); somewhere in your code to print output to the terminal. Without explicit output commands, Node.js runs silently even if the code works correctly.
Can I run JavaScript files by double-clicking them?
Not directly — double-clicking a .js file usually opens it in a text editor. You must run it through the terminal with the node command. Some developers create batch files or shell scripts that run the node command automatically, but the standard method is typing the command yourself.