Important npm Commands
Last Updated :
19 Mar, 2024
Node Package Manager (npm) stands at the core of JavaScript development, serving as a robust package manager for handling dependencies, project initialization, and script execution. Understanding the essential npm commands is important for managing dependencies and automating tasks. In this article, we’ll explore the core npm commands every developer should know and how to use them effectively.
Note: It is essential to have Node and NPM installed in your system
What is NPM?
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integrated into Node.js projects to extend their functionality.
Features of NPM:
- Package Management: NPM makes it easy to manage local dependencies of a project. Developers can specify and install dependencies, ensuring that everyone working on the project has the correct versions.
- Version Control: NPM helps manage package versions, allowing developers to specify version ranges for dependencies. This ensures compatibility and stability within projects.
- Scripts Execution: NPM can run scripts defined in a project’sÂ
package.json
 file, automating tasks like testing, building, or deploying.
- Publishing Packages: NPM provides a platform for developers to publish their packages, making them available to the global JavaScript community.
- Discoverability: Through the NPM registry, developers can easily discover packages that can be reused in their projects, fostering a culture of code sharing and reuse.
Essential NPM Commands:
1. Initialization:
The npm init
command initializes a new Node.js project by creating a package.json
file. It prompts you to enter essential project details such as name, version, description, entry point, and more, guiding you through the project setup process.
Syntax:
npm init -y
2. Installing Dependencies:
The npm install
command installs dependencies listed in the package.json
file. By default, npm installs dependencies locally in the node_modules
directory.
Syntax:
npm install <package-name>
Here the package used is “express”
For development-specific dependencies, use the –save-dev flag:
npm install <package-name> --save-dev
Package used “typescript”
3. Global Installations:
The npm install --global
command installs packages globally, making them accessible across all projects. Global packages often comprise command-line tools or utilities that enhance developer productivity.
Syntax:
npm install -g <package-name>
package used “nodemon”
4. Updating Packages:
The npm update
command updates packages to their latest versions according to the version range specified in the package.json
file. It ensures that your project utilizes the latest features and patches while maintaining compatibility with existing dependencies.
Syntax:
npm update <package-name>
Package used “express”
5. Uninstalling Packages:
The npm uninstall
command removes a package from the project. It uninstalls the specified package from the node_modules
directory and updates the package.json
file to reflect the removal of the dependency.
Syntax:
npm uninstall <package-name>
6. Listing Installed Packages:
The npm ls command is used to list all the installed packages.
Syntax:
npm ls
For a global perspective:
npm ls -g
7. Running Scripts:
The npm run
command executes scripts defined in the scripts
section of the package.json
file. It enables developers to define custom scripts for various tasks such as building, testing, linting, and running the application.
Syntax:
npm run <script-name>
8. Checking Outdated Packages:
The npm outdated
command checks for outdated packages in the project. It displays a list of installed packages that have newer versions available, allowing developers to update dependencies and stay up-to-date with the latest releases.
Syntax:
npm outdated
Share your thoughts in the comments
Please Login to comment...