4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to run Javascript in Visual Studio Code?

By 4Geeks

Run any JavaScript file from Visual Studio Code with Node.js, npm scripts, the debugger or the browser, and fix the errors you will meet on the way.

6 min readLast updated
Run JavaScript in VS Code icon

How do you run JavaScript in VS Code?

Short answer: install Node.js, open your project in VS Code, open the terminal with Ctrl + ` (backtick) and run:

bash
node app.js

VS Code is the editor and Node.js is what runs the code. If node -v prints a version number in the terminal, you are ready.

node app.js runs the file, npm start runs it with a short command and node --watch runs it again every time you save.

Every way to run JavaScript in VS Code

MethodHowBest for
Terminal with Node.jsnode app.jsAny script, the most common way
npm scriptnpm startProjects with a package.json
Watch modenode --watch app.jsRerunning the file every time you save
Run and DebugF5 or Ctrl + F5 and choose Node.jsStopping at breakpoints and inspecting variables
Code Runner extensionCtrl + Alt + NRunning a file or a selection with one shortcut
In the browserAn index.html with <script src="app.js"> and the Live Server extensionCode that uses document or window

Step 1: Install Node.js and check it

  1. Download the LTS version from nodejs.org and run the installer.
  2. Close and open VS Code again so it finds Node.js.
  3. Open the terminal with Ctrl + ` or View > Terminal and type:
bash
node -v
npm -v

Both commands should print a version number. If you see 'node' is not recognized or command not found: node, Node.js is not installed or VS Code was open while you installed it. If you manage several versions, you can also install Node.js with NVM.

Step 2: Create a JavaScript file and run it

  1. Open a folder with File > Open Folder.
  2. Create a file called app.js:
javascript
const name = "Ana";
console.log(`Hello, ${name}!`);
  1. Save it and run it in the terminal:
$ node app.js
Hello, Ana!

The terminal must be in the same folder as the file. If it isn't, move there with cd or write the path, such as node src/app.js.

Career · AI Engineering

You can run JavaScript in VS Code, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Step 3: Add an npm start script

For projects, save the command in package.json so everyone runs the code the same way:

bash
npm init -y
npm pkg set scripts.start="node app.js"
npm start

npm init -y creates package.json, and npm pkg set adds the script for you. The important part of the file looks like this:

json
{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node app.js"
  }
}

Now npm start runs your file:

$ npm start
 
> my-app@1.0.0 start
> node app.js
 
Hello, Ana!

Rerun the file every time you save

Instead of typing node app.js after each change, use watch mode:

bash
node --watch app.js

Node.js runs the file, waits, and runs it again every time you save. Press Ctrl + C to stop it.

Run JavaScript with the VS Code debugger

VS Code includes a JavaScript debugger, so you don't need an extension:

  1. Open app.js and click to the left of a line number to add a red breakpoint.
  2. Press F5 (or go to Run > Start Debugging) and choose Node.js if VS Code asks.
  3. The code stops at the breakpoint. Hover over variables to see their values, and use the toolbar to continue line by line.

Ctrl + F5 (Run > Run Without Debugging) runs the file without stopping. The output appears in the Debug Console.

Run JavaScript with Code Runner

The Code Runner extension adds a play button to the top right of the editor:

  1. Open the Extensions view with Ctrl + Shift + X, search for Code Runner and install it.
  2. Open your file and press Ctrl + Alt + N (Ctrl + Option + N on macOS), or click the play button.

It still uses Node.js, so you need it installed. The result appears in the Output panel, which can't read keyboard input. To use the terminal instead, turn on the Run In Terminal setting of Code Runner.

Run JavaScript in the browser from VS Code

Code that uses document, window or alert() needs a browser, not Node.js. Create an index.html next to your script:

html
<!DOCTYPE html>
<html>
  <body>
    <h1>My page</h1>
    <script src="app.js"></script>
  </body>
</html>

Open the file in your browser, or install the Live Server extension, right-click index.html and choose Open with Live Server. The page reloads every time you save. You will see console.log() in the browser's developer tools (F12 > Console).

Use AI to fix errors when running JavaScript

An assistant like ChatGPT, Claude or GitHub Copilot, which is built into VS Code, can explain errors from your terminal. These three prompts are ready to copy.

1. Explain an error

I run node app.js in the VS Code terminal on [Windows / macOS / Linux] and get this error:
[paste the error]. Explain what it means and how to fix it.

2. Set up a project

Create a package.json for a Node.js project with a start script for app.js and a dev script
that reruns the file when I save. Explain each line.

3. Node.js or browser

This is my JavaScript file: [paste the code]. Should I run it with Node.js or in the browser?
Explain why and show me how to do it from VS Code.

Read the answer before you run any command it suggests.

Learn with a mentor

From running node app.js to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems when running JavaScript in VS Code

What you seeWhy it happensFix
'node' is not recognized as an internal or external commandNode.js is not installed, or VS Code was open while you installed itInstall Node.js and restart VS Code
zsh: command not found: nodeNode.js is not installed or not in your PATHInstall it, or load it with NVM
Error: Cannot find module '.../app.js'The terminal is in another folder, or the name is wrongUse cd to the folder, or check the file name
ReferenceError: document is not definedYou ran browser code with Node.jsOpen it in the browser with an HTML file
Nothing appears when you run the fileYou didn't save, or there is no console.log()Save with Ctrl + S and print something
npm error Missing script: "start"package.json has no start scriptRun npm pkg set scripts.start="node app.js"
Code Runner can't read inputThe Output panel is read-onlyTurn on Run In Terminal in Code Runner settings
Free download

Running JavaScript in VS Code in a 2-page cheat sheet

Node.js setup, node app.js, npm start, watch mode, the debugger, Code Runner, Live Server, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to run JavaScript in VS Code cheat sheet

2 pages · PDF · 4Geeks

  • Every way to run a JavaScript file from VS Code
  • The shortcuts and commands you need, in one place
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about running JavaScript in VS Code

Want to go from running JavaScript to building AI apps, with real projects and mentors?

See the AI Engineering program