4Geeks logo
4Geeks logo

Bootcamps

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Academy

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
← Back to How to's
Edit on Github

How to run Javascript in Visual Studio Code?

Written by:

To be able to run javascript in Visual Studio Code, we need to have installed NodeJS on our system and have 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 (Finder 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 v16.51.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 keyword for NodeJs 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:
1$ 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

1 "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:

1npm run start

How to run javascript on Visual Studio