Skip to main content

Comparison of pip and npm Commands

Comparison of pip and npm Commands
Deepak Kamboj
Senior Software Engineer
4 min read
Python

This document compares the commands used by pip (Python package manager) and npm (Node.js package manager) to perform similar tasks.

Installation Commands

Taskpip Commandnpm CommandDescription
Install a packagepip install <package_name>npm install <package_name>Installs the specified package globally or locally within a project.
Install packages from a filepip install -r requirements.txtnpm installInstalls all packages listed in the requirements file (requirements.txt for pip, package.json for npm).
Install a specific versionpip install <package_name>==<version>npm install <package_name>@<version>Installs the specified version of the package.
Install globallypip install <package_name> (with --user)npm install -g <package_name>Installs the package globally, making it available system-wide.
Install a package from GitHubpip install git+https://github.com/user/repo.gitnpm install git+https://github.com/user/repo.gitInstalls a package directly from a GitHub repository.
Install a package from a local pathpip install ./path/to/packagenpm install ./path/to/packageInstalls a package from a local directory.

Uninstallation Commands

Taskpip Commandnpm CommandDescription
Uninstall a packagepip uninstall <package_name>npm uninstall <package_name>Uninstalls the specified package.
Uninstall globallypip uninstall <package_name> (with --user)npm uninstall -g <package_name>Uninstalls the package globally.

Listing Installed Packages

Taskpip Commandnpm CommandDescription
List installed packagespip listnpm listLists all installed packages in the current environment or project.
List globally installed packagespip list --usernpm list -gLists all globally installed packages.

Updating Packages

Taskpip Commandnpm CommandDescription
Update a packagepip install --upgrade <package_name>npm update <package_name>Updates the specified package to the latest version.
Update all packagesN/Anpm updateUpdates all packages in the node_modules directory to their latest versions according to the version ranges specified in package.json.

Package Information

Taskpip Commandnpm CommandDescription
Show package detailspip show <package_name>npm info <package_name>Displays detailed information about the specified package.
Search for a packagepip search <package_name>npm search <package_name>Searches the package index for a package by name.

Dependency Management

Taskpip Commandnpm CommandDescription
Install dependencies from a filepip install -r requirements.txtnpm installInstalls dependencies listed in requirements.txt for pip or package.json for npm.
Check for outdated packagespip list --outdatednpm outdatedLists all outdated packages.

Project Initialization

Taskpip Commandnpm CommandDescription
Initialize a projectN/A (Manual creation of files)npm initInitializes a new Node.js project and creates a package.json file.
Save installed packages to a filepip freeze > requirements.txtnpm shrinkwrap or npm install --package-lockSaves the current list of installed packages to a requirements.txt for pip or package-lock.json for npm.

Configuration and Scripts

Taskpip Commandnpm CommandDescription
Run scripts defined in config fileN/Anpm run <script_name>Runs a script defined in the scripts section of package.json.
View or set config variablespip config get/setnpm config get/setGets or sets configuration variables.

Virtual Environments

Taskpip Commandnpm CommandDescription
Create a virtual environmentpython -m venv <env_name>npx <package_name>Creates a virtual environment in Python; for npm, a similar effect can be achieved using npx.
Activate a virtual environmentsource <env_name>/bin/activate (Linux/macOS) or <env_name>\Scripts\activate (Windows)N/AActivates a Python virtual environment. NPM does not have a direct equivalent.

Cleaning Up

Taskpip Commandnpm CommandDescription
Clean up cachepip cache purgenpm cache clean --forceCleans the local cache of downloaded files.

Lock Files

Taskpip Commandnpm CommandDescription
Generate a lock fileN/A (pip does not natively support lock files)npm shrinkwrap or npm install --package-lockGenerates a package-lock.json file that locks the versions of installed dependencies.

Environment Variables

Taskpip Commandnpm CommandDescription
List environment variablespip config listnpm config listLists environment variables related to pip or npm configuration.
Set environment variablepip config set <name> <value>npm config set <name> <value>Sets an environment variable.

Conclusion

pip and npm are both powerful tools for managing packages in their respective ecosystems. While there are many similarities in their command structures, there are also differences that reflect the distinct environments they operate in (Python vs. Node.js).