Node Package Manager (NPM) is a crucial tool for web developers, helping manage dependencies, automate tasks, and streamline project workflows. Whether you're a beginner or an experienced developer, mastering essential NPM commands can boost your efficiency. In this guide, we'll explore the most useful NPM commands every web developer should know.
Table of Contents
1. Installing and Managing Packages
1.1 Install a Package Locally
1.2 Install a Package Globally
1.3 Install Dev Dependencies
2. Managing Packages
2.1 Update Packages
2.2 Check for Outdated Packages
2.3 Upgrade to Latest Version
3. Removing and Cleaning Dependencies
3.1 Uninstall a Package
3.2 Remove Unused Packages
4. Running Scripts
4.1 Run Custom Scripts
4.2 Start a Project
5. Initializing and Managing Projects
5.1 Initialize a Project
5.2 View Installed Packages
### 6. Managing Cache and Debugging
6.1 Clear Cache
6.2 Audit for Security Issues
7. Conclusion
1. Installing and Managing Packages
1.1 Install a Package Locally
npm install package-name
or
npm i package-name
Installs a package in the node_modules
folder and adds it to package.json
under dependencies.
Example:
npm install express
Installs Express.js for building web applications.
1.2 Install a Package Globally
npm install -g package-name
Global installation makes the package accessible across all projects.
Example:
npm install -g nodemon
Installs Nodemon, a tool that automatically restarts the server on file changes.
1.3 Install Dev Dependencies
npm install package-name --save-dev
or
npm i package-name -D
Installs packages needed for development but not in production.
Example:
npm install eslint --save-dev
Installs ESLint for code linting.
2. Managing Packages
2.1 Update Packages
npm update package-name
Updates a specific package to the latest compatible version.
Example:
npm update react
Updates React to the latest version allowed by package.json
.
2.2 Check for Outdated Packages
npm outdated
Lists all outdated packages in your project.
2.3 Upgrade to Latest Version
npm upgrade
or
npm install package-name@latest
Upgrades all dependencies or a specific package to the latest version.
Example:
npm install lodash@latest
Upgrades Lodash to the latest release.
3. Removing and Cleaning Dependencies
3.1 Uninstall a Package
npm uninstall package-name
or
npm remove package-name
Removes a package from node_modules
and package.json
.
Example:
npm uninstall moment
Removes Moment.js from the project.
3.2 Remove Unused Packages
npm prune
Deletes unnecessary packages not listed in package.json
.
4. Running Scripts
4.1 Run Custom Scripts
npm run script-name
Executes a script defined in package.json
.
Example (package.json
):
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
To run the development script:
npm run dev
4.2 Start a Project
npm start
Runs the start
script from package.json
.
5. Initializing and Managing Projects
5.1 Initialize a Project
npm init
Starts an interactive setup to create a package.json
file.
For a quicker setup without prompts:
npm init -y
Creates package.json
with default values.
5.2 View Installed Packages
npm list
Shows installed dependencies in the project.
For globally installed packages:
npm list -g --depth=0
6. Managing Cache and Debugging
6.1 Clear Cache
npm cache clean --force
Clears the NPM cache to fix installation issues.
6.2 Audit for Security Issues
npm audit
Scans dependencies for vulnerabilities.
To fix issues automatically:
npm audit fix
Conclusion
Mastering NPM commands is essential for managing packages efficiently in web development. Whether you're installing dependencies, running scripts, or debugging issues, these commands streamline your workflow. Bookmark this guide and improve your development experience!
Top comments (0)