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

How to run Javascript in Visual Studio Code?

You can run Javascript in the Visual Studio Code through the terminal if you have node installed (node filename.js).
5 min readLast updated

Short answer: To run a JavaScript file in Visual Studio Code, install Node.js (check it with node -v in Command Prompt on Windows or Terminal on macOS), open a new terminal in VS Code and run node app.js. For a reusable command, create package.json with npm init -y, add the script "start": "node app.js" and run npm run start.

To be able to run javascript in Visual Studio Code, we need to have NodeJS installed on our system and a JavaScript file created with some content.

How to run javascript on Visual Studio

Before being able to run Javascript on Visual Studio Code, we need to install Visual Studio Code and NodeJS. Visual Studio Code is the IDE (Integrated Development Environment) we will be using throughout the article and NodeJS is the engine that allows Javascript to run locally.

Installing Visual Studio Code

Visual Studio Code is ready to be used, but we still need NodeJs to run Javascript locally

Check if NodeJS is installed already and the version

  • Open command prompt

    • Start menu (Spotlight if you are using mac).
    • Write cmd (Terminal if you are using mac).
    • And open it.
  • Type on the command windows node -v

If NodeJS is present in the system, you´ll see something like v20.11.1 being that the installed version of Node (It could be other version, no worries).

Installing NodeJs

  • Go to https://nodejs.org

  • Download the recommended version.

  • Run the installer.

  • Accept terms and conditions and next, next, next...

Now that we have NodeJS installed on our system, we can run Javascript locally and Visual Studio Code as our IDE, but how to run Javascript on Visual Studio Code?

Let´s set up our Javascript project

  • Start Visual Studio Code.

How to run javascript on Visual Studio

  • Open a new Terminal.

How to run javascript on Visual Studio

  • Write on the terminal npm init -y.

How to run javascript on Visual Studio

  • npm stands for Node Package Manager

  • init is the npm command to create a new project.

  • -y is to pass as a default all values.

How to run javascript on Visual Studio

  • If successful, a package.json file will be created on your project folder.

How to run javascript on Visual Studio

  • Right click on the file explorer (where the package.json is displayed) and select new file.

  • We are naming it app.js (js is a Javascript extension).

How to run javascript on Visual Studio

  • Now let´s write some Javascript code on our app.js file.

How to run javascript on Visual Studio

  • Go on, write on the terminal:
bash
$ node app

Remember to take out the "$"

And you´ll see on the terminal the console.log() message displayed.

How to run javascript on Visual Studio

Still, I prefer to do it another way, since it allows for a more robust and personalized way to run our app.js file.

Remember the package.json we created with npm init -y? Well, we are editing now. We will make that, every time we type npm run start on the Visual Studio terminal, it´ll execute our app.js file.

  • Select package.json to edit it.

  • Add

json
	"start" : "node app.js"

How to run javascript on Visual Studio

Careful! We added a , at the end of line 7, if the , is missing, the JSON format is not correct and will throw errors / won´t work.

Now, we can run our Javascript project like this:

bash
npm run start

How to run javascript on Visual Studio

Running JavaScript with npm run start in Visual Studio Code mirrors the workflow used in professional projects. To practice it at a larger scale, a programming bootcamp pairs hands-on JavaScript projects with mentor feedback.

Frequently asked questions

Can I run JavaScript in VS Code without installing Node.js?

VS Code itself cannot execute JavaScript without a runtime. Without Node.js, link the file from an HTML page with a <script src="app.js"></script> tag and open that page in a web browser; the console.log() output appears in the browser's developer tools console. Node.js is needed only to run scripts directly from the VS Code terminal.

How do I run a JavaScript file with the VS Code debugger?

Open the file and press F5, or use the Run and Debug view, then choose Node.js when VS Code asks for a debugger. VS Code includes a built-in JavaScript debugger, so the program runs and its output appears in the Debug Console. Click to the left of a line number to add a breakpoint and pause execution there.

Why does VS Code say 'node' is not recognized?

The terminal cannot find Node.js in the PATH. Either Node.js is not installed, or VS Code was already open during the installation and kept the old environment. Close every VS Code window, open it again and run node -v. If the error remains, reinstall Node.js and make sure the option that adds it to PATH stays selected.

Is npm start different from running node app.js directly?

npm start is a shortcut for npm run start, which runs whatever command the start script in package.json defines, here node app.js. The result is the same, but the script lives with the project, so anyone can run it without knowing the entry file, and npm adds locally installed tools from node_modules/.bin to the PATH.