NPM Command Cheat Sheet
Intro to NPM
Node Package Manager (NPM) is a command-line tool integral to the Node.js environment, acting as a package manager to install, update, and manage libraries, packages, and modules for Node.js applications. It's a key tool for every Node.js developer, regardless of their experience level.
The primary purpose of NPM is managing external libraries and dependencies common in Node.js projects. Beyond that, it facilitates sharing and distributing code, running scripts, and creating new projects, showcasing its versatility in the Node.js ecosystem.
This guide is intended to be a cheat sheet, a quick reference that you can return to time and again to understand and make the most of NPM's features and capabilities.
NPM and Its Importance in Node.js
NPM bridges your application with a global developer community, offering access to a vast array of packages and libraries. This enables leveraging collective solutions and expertise instead of starting from scratch.
The strength of NPM lies in its simplicity and efficiency, streamlining the management of necessary packages for your application, ranging from a handful to hundreds depending on the project's scope. Declare what you need, and NPM handles the rest—fetching, installing, and updating packages as needed.
Understanding NPM Commands
NPM commands are the actions that you can perform using the NPM tool. These are entered into the command line and are typically followed by one or more arguments. For example, in the command npm install express
, "install" is the command, and "express" is the argument.
There are a host of NPM commands available, each designed to perform a specific action. While it's beneficial to know and understand all of them, in practice, you'll find that there are a few you'll use much more frequently than others. These are the ones we'll focus on in this guide.
Here's a table summarizing the most commonly used NPM commands and a brief description of what they do:
Command | Description |
---|---|
npm init | Initializes a new NPM project and creates a new package.json file in the directory. |
npm install | Installs all dependencies listed in the package.json file. |
npm install [package] | Installs a specific package and adds it to the list of dependencies in the package.json file. |
npm install [package] --save-dev | Installs a specific package as a development dependency. |
npm uninstall [package] | Removes a specific package from the node modules and the package.json file. |
npm update | Updates all packages to their latest versions, as specified by the version range in the package.json file. |
npm update [package] | Updates a specific package to its latest version. |
npm ls | Displays the dependency tree for the current project, showing all installed packages and their dependencies. |
npm run [script] | Runs a script defined in the scripts section of the package.json file. |
npm test | Runs the test script defined in the scripts section of the package.json file. |
npm publish | Publishes a package to the NPM registry. |
npm version [update_type] | Updates the version number in the package.json file, based on the specified update type (major, minor, or patch). |
npm audit | Checks for known vulnerabilities in your installed packages and suggests fixes. |
npm cache clean --force | Clears the NPM cache, often used to solve problems with installing packages. |
npm outdated | Checks for outdated packages. |
npm root | Displays the root directory of where your packages are stored. |
npm config get prefix | Gets the directory where global packages are installed. |
This table offers a high-level overview of these commands and their functions, but each command has more depth and complexity than can be covered here. Understanding the intricacies of each command is vital for effectively and efficiently working with NPM.
Deep Dive into NPM Commands
In the following sections, we'll take a more detailed look at each of these commands, expanding on their uses, their syntax, and providing examples of how they can be used.
npm init
npm init
is the command used to create a new NPM project. When you run this command, NPM will prompt you to provide some information about your project, such as its name, version, description, main file, test command, Git repository, keywords, author, and license.
These details are then used to create a package.json
file in your current directory, which serves as the manifest file for your project. This file contains metadata about your project and lists its dependencies, allowing NPM to identify what packages need to be installed for your project to work.
You can also use npm init -y
to automatically fill out the package.json
file with default values. This is handy when you want to quickly set up a project without being prompted for input.
Example usage:
$ npm init
npm install
npm install
is one of the most commonly used NPM commands. It's used to install packages that your project depends on. This command reads the package.json
file to identify the project's dependencies and installs them.
If you specify a package name after the npm install
command, NPM will install that particular package. By default, it installs the latest version of the package. However, you can also specify a version number, a tag, or a version range if you need a specific version of the package.
Example usage:
$ npm install
$ npm install express
$ npm install [email protected]
$ npm install express@latest
npm install [package] --save-dev
While the npm install [package]
command installs a package as a production dependency, there are times when you need to install a package that's only necessary for development. For example, testing libraries, transpilers, and linters are all packages that are typically only needed during the development process and not in production.
This is where the npm install [package] --save-dev
command comes in. It installs the specified package and adds it to the "devDependencies" section of the package.json
file, indicating that it's only a development dependency.
Example usage:
$ npm install jest --save-dev
npm uninstall
Just as you can install packages with NPM, you can also remove them using the npm uninstall
command followed by the name of the package you want to remove.
This command removes the specified package from the node_modules directory and also updates the package.json
and package-lock.json
files to reflect the removal of the package.
Example usage:
$ npm uninstall express
npm update
Over time, the packages you're using in your project may be updated by their maintainers to include new features, fix bugs, or patch security vulnerabilities. It's important to keep your packages updated to benefit from these changes.
The npm update
command helps you do this. When run without any arguments, this command checks all the packages listed in the package.json
file and updates them to the latest versions as per the defined range.
You can also update a specific package to its latest version by specifying the package name after the npm update
command.
Example usage:
$ npm update
$ npm update express
npm ls
The npm ls
command is used to list the installed packages and their dependencies. This command is particularly useful when you want to see the versions of your installed packages or their dependencies.
By default, npm ls
displays the dependency tree of the current project. However, you can specify a package name to see the version of that package and its dependencies.
Example usage:
$ npm ls
$ npm ls express
npm run [script]
The npm run
command is used to execute scripts defined in the scripts
section of the package.json
file. This can include scripts for building your application, running tests, starting your application, and more.
To run a script, you simply need to follow the npm run
command with the name of the script you want to run.
Example usage:
$ npm run start
$ npm run test
npm test
The npm test
command is a shortcut for npm run test
. It's used to run the test script defined in the scripts
section of the package.json
file.
Example usage:
$ npm test
npm publish
If you've developed a Node.js package that you want to share with others, you can use the npm publish
command to publish it to the NPM registry. Once your package is published, others can install it using the npm install
command.
Before you can publish a package, you need to create an account on the NPM website and log in to your account using the npm login
command.
Example usage:
$ npm publish
npm version [update_type]
The npm version
command is used to update the version number of your package. This command updates the version number in the package.json
file and also creates a new Git tag with the updated version number.
You need to specify the update type after the npm version
command, which can be "patch", "minor", or "major", depending on the nature of the changes made in the new version.
Example usage:
$ npm version patch
$ npm version minor
$ npm version major
npm audit
The npm audit
command is used to identify known security vulnerabilities in your project's dependencies. When you run this command, NPM checks the packages in your package.json
file against the Node Security Platform database to find vulnerabilities.
The npm audit
command not only identifies vulnerabilities but also provides detailed information about each vulnerability and suggests commands to fix them.
Example usage:
$ npm audit
npm cache clean --force
The npm cache clean --force
command is used to clear the NPM cache. The cache is a storage for data that can be used again in the future. NPM uses a cache to store packages that were installed, so it doesn't need to fetch them again when they're needed.
Clearing the cache can help to solve some issues with installing or updating packages.
Example usage:
$ npm cache clean --force
npm outdated
The npm outdated
command is used to check for outdated packages. When you run this command, NPM checks the packages in your package.json
file and compares the version you have installed with the latest version available on the NPM registry.
If there are any outdated packages, this command will list them along with the current version you have, the latest version available, and the version defined in your package.json
file.
Example usage:
$ npm outdated
npm root
The npm root
command is used to find out the root directory where your packages are installed. This can be useful if you need to know where a package has been installed or if you're having issues with a package and need to inspect its files.
Example usage:
$ npm root
npm config get prefix
The npm config get prefix
command is used to get the directory where global packages are installed. This is typically /usr/local
on Unix systems and C:\Users\<username>\AppData\Roaming\npm
on Windows.
Example usage:
$ npm config get prefix
Conclusion
This comprehensive guide provides an in-depth overview of the most commonly used NPM commands. Whether you're just getting started with Node.js or you're an experienced developer, having a solid understanding of these commands can make your development process more efficient and enjoyable.
NPM is a powerful tool that offers a wide array of features. Knowing how to leverage these features can greatly enhance your Node.js development experience. So, use this guide as a reference to understand and make the most of NPM's features and capabilities.
And remember, the official NPM documentation is always there to provide more detailed information and help you explore other commands and features not covered in this guide.
With these commands at your disposal, you're well-equipped to manage your Node.js project's dependencies and ensure your application runs smoothly.
Additional Resources
GitHub - npm/cli: a JavaScript package manager
VS Code Shortcuts and Essential Extensions