Installing and Running npm Commands in VS Code

Complete Guide: Installing and Running npm Commands in VS Code

1. Introduction

This guide provides step-by-step instructions on how to install, configure, and run npm commands in Visual Studio Code (VS Code). npm (Node Package Manager) is a package manager for JavaScript, used to install and manage dependencies for Node.js projects.


2. Prerequisites

Before running npm commands in VS Code, ensure you have the following:

  • Node.js installed (which includes npm)
  • VS Code installed

Checking if Node.js and npm are Installed

Open a terminal or command prompt and run:

node -v   # Checks Node.js version
npm -v    # Checks npm version

If Node.js and npm are installed, these commands will return their respective versions.


3. Installing Node.js and npm

If Node.js and npm are not installed, follow these steps:

Windows/macOS/Linux:

  1. Download the latest Node.js LTS version from https://nodejs.org/.
  2. Run the installer and follow the setup instructions.
  3. Restart your computer (optional but recommended).
  4. Verify installation by running:
    node -v
    npm -v

4. Opening a Project in VS Code

  1. Open VS Code.
  2. Click on File > Open Folder and select your project folder.
  3. Open the integrated terminal in VS Code by pressing Ctrl + ` (backtick) or going to View > Terminal.

5. Initializing a Node.js Project

If your project does not have a package.json file, you need to initialize it by running:

npm init -y

This creates a package.json file with default values.


6. Installing npm Packages

Installing a Single Package

To install a package, use:

npm install <package-name>

Example:

npm install express

Installing Multiple Packages

npm install lodash axios moment

Installing a Package Globally

Use the -g flag to install a package globally:

npm install -g nodemon

Installing Development Dependencies

For dependencies needed only for development, use the --save-dev flag:

npm install nodemon --save-dev

Installing All Dependencies from package.json

If you have a package.json file, install all dependencies with:

npm install

7. Running npm Scripts in VS Code

If your package.json file has scripts defined, such as:

"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}

Run these scripts using:

npm run start   # Runs the "start" script
npm run dev     # Runs the "dev" script

8. Updating and Removing npm Packages

Updating a Package

To update a specific package:

npm update <package-name>

To update all packages:

npm update

Removing a Package

To uninstall a package:

npm uninstall <package-name>

9. Fixing Common Issues

npm Command Not Recognized

If you get an error like ‘npm’ is not recognized as an internal or external command, try:

  1. Restart VS Code.
  2. Ensure Node.js and npm are correctly installed.
  3. Check npm’s location by running:
    where npm   # Windows
    which npm   # macOS/Linux
  4. Add npm to the system’s PATH (Windows users can check C:\Program Files\nodejs\ in the system environment variables).

Permission Issues on macOS/Linux

If you encounter permission errors, use sudo :

sudo npm install -g <package-name>

Or configure npm to avoid permission issues:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH="$HOME/.npm-global/bin:$PATH"
source ~/.profile

10. Conclusion

By following this guide, you should be able to:

  • Install and configure npm in VS Code
  • Install, update, and remove npm packages
  • Run npm scripts
  • Troubleshoot common issues

Now you’re ready to efficiently manage Node.js projects in VS Code! 🚀

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *