11 Mar
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 t...
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.
Before running npm commands in VS Code, ensure you have the following:
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.
If Node.js and npm are not installed, follow these steps:
node -v
npm -v
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.
To install a package, use:
npm install <package-name>
Example:
npm install express
npm install lodash axios moment
Use the
-g
flag to install a package globally:
npm install -g nodemon
For dependencies needed only for development, use the
--save-dev
flag:
npm install nodemon --save-dev
If you have a
package.json
file, install all dependencies with:
npm install
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
To update a specific package:
npm update <package-name>
To update all packages:
npm update
To uninstall a package:
npm uninstall <package-name>
If you get an error like ‘npm’ is not recognized as an internal or external command, try:
where npm # Windows
which npm # macOS/Linux
C:\Program Files\nodejs\
in the system environment variables).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
By following this guide, you should be able to:
Now you’re ready to efficiently manage Node.js projects in VS Code! 🚀