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:
node app.jsVS 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
| Method | How | Best for |
|---|---|---|
| Terminal with Node.js | node app.js | Any script, the most common way |
| npm script | npm start | Projects with a package.json |
| Watch mode | node --watch app.js | Rerunning the file every time you save |
| Run and Debug | F5 or Ctrl + F5 and choose Node.js | Stopping at breakpoints and inspecting variables |
| Code Runner extension | Ctrl + Alt + N | Running a file or a selection with one shortcut |
| In the browser | An index.html with <script src="app.js"> and the Live Server extension | Code that uses document or window |
Step 1: Install Node.js and check it
- Download the LTS version from nodejs.org and run the installer.
- Close and open VS Code again so it finds Node.js.
- Open the terminal with Ctrl + ` or View > Terminal and type:
node -v
npm -vBoth 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
- Open a folder with File > Open Folder.
- Create a file called
app.js:
const name = "Ana";
console.log(`Hello, ${name}!`);- 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.
