Run NPM commands/scripts within Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# Table of Contents

  1. Run NPM commands/scripts within Visual Studio Code
  2. Running NPM commands from the Terminal in VS Code

# Run NPM commands/scripts within Visual Studio Code

To run NPM commands/scripts within Visual Studio Code:

  1. Right-click on the OUTLINE or TIMELINE menus at the bottom of the left sidebar.

right click select npm scripts

  1. Make sure the NPM Scripts option is checked.

make sure npm scripts option is checked

  1. Left-click on the NPM Scripts menu to expand it.

  2. Hover over a script and click on the > Run button to run it.

run npm script

Make sure that you have a package.json file in the root directory of your project.

If you need to generate a package.json file, run the following command.

shell
npm init -y

The scripts that are shown in the NPM Scripts section in the sidebar are the ones that are present in the scripts object of your package.json file.

Here is the scripts section of my package.json file.

package.json
{ "scripts": { "dev": "npx nodemon index.js", "test": "jest --testTimeout=30000", "clear-jest-cache": "jest --clearCache" } }

And here are the scripts in the NPM Scripts section in my sidebar.

npm scripts in left sidebar

Here is a short clip that demonstrates how to run an NPM script from your package.json file within VS Code.

run npm scripts in vscode

Once you click on the Run > button, a new terminal window opens where the script is run.

If you aren't able to show the NPM Scripts menu in your left sidebar:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
  1. Type NPM.
  2. Select Explorer: Focus on NPM Scripts View.

explorer focus on npm scripts view

Once you select the command, your editor will focus the NPM Scripts menu in the left sidebar.

Hover over a script and click on the Run > icon.

# Running NPM commands from the Terminal in VS Code

Alternatively, you can run NPM commands directly in the integrated terminal in VS Code:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type toggle terminal and select View: Toggle Terminal.

vscode open terminal

You can also open the terminal by using a keyboard shortcut:

  • on Windows and Linux: Ctrl + ` (backtick).
  • on macOS: Ctrl + ` (backtick).
  1. Issue your npm commands.
shell
npm --version npm install axios

run npm commands in integrated terminal in vs code

Make sure that you have Node.js installed to be able to run NPM commands in VS Code.

I've written an article on how to install Node.js on Windows using the official installer.

You can also use the NVM package to install and manage your Node.js version:

Make sure to restart Visual Studio Code after you install Node.js.

If you get errors when running NPM commands in VS Code and you have Node.js installed, try to run VS Code as an administrator before issuing the commands.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.