This document compares the commands used by pip
(Python package manager) and npm
(Node.js package manager) to perform similar tasks.
Installation Commands
Task | pip Command | npm Command | Description |
---|
Install a package | pip install <package_name> | npm install <package_name> | Installs the specified package globally or locally within a project. |
Install packages from a file | pip install -r requirements.txt | npm install | Installs all packages listed in the requirements file (requirements.txt for pip, package.json for npm). |
Install a specific version | pip install <package_name>==<version> | npm install <package_name>@<version> | Installs the specified version of the package. |
Install globally | pip install <package_name> (with --user ) | npm install -g <package_name> | Installs the package globally, making it available system-wide. |
Install a package from GitHub | pip install git+https://github.com/user/repo.git | npm install git+https://github.com/user/repo.git | Installs a package directly from a GitHub repository. |
Install a package from a local path | pip install ./path/to/package | npm install ./path/to/package | Installs a package from a local directory. |
Uninstallation Commands
Task | pip Command | npm Command | Description |
---|
Uninstall a package | pip uninstall <package_name> | npm uninstall <package_name> | Uninstalls the specified package. |
Uninstall globally | pip uninstall <package_name> (with --user ) | npm uninstall -g <package_name> | Uninstalls the package globally. |
Listing Installed Packages
Task | pip Command | npm Command | Description |
---|
List installed packages | pip list | npm list | Lists all installed packages in the current environment or project. |
List globally installed packages | pip list --user | npm list -g | Lists all globally installed packages. |
Updating Packages
Task | pip Command | npm Command | Description |
---|
Update a package | pip install --upgrade <package_name> | npm update <package_name> | Updates the specified package to the latest version. |
Update all packages | N/A | npm update | Updates all packages in the node_modules directory to their latest versions according to the version ranges specified in package.json . |
Task | pip Command | npm Command | Description |
---|
Show package details | pip show <package_name> | npm info <package_name> | Displays detailed information about the specified package. |
Search for a package | pip search <package_name> | npm search <package_name> | Searches the package index for a package by name. |
Dependency Management
Task | pip Command | npm Command | Description |
---|
Install dependencies from a file | pip install -r requirements.txt | npm install | Installs dependencies listed in requirements.txt for pip or package.json for npm. |
Check for outdated packages | pip list --outdated | npm outdated | Lists all outdated packages. |
Project Initialization
Task | pip Command | npm Command | Description |
---|
Initialize a project | N/A (Manual creation of files) | npm init | Initializes a new Node.js project and creates a package.json file. |
Save installed packages to a file | pip freeze > requirements.txt | npm shrinkwrap or npm install --package-lock | Saves the current list of installed packages to a requirements.txt for pip or package-lock.json for npm. |
Configuration and Scripts
Task | pip Command | npm Command | Description |
---|
Run scripts defined in config file | N/A | npm run <script_name> | Runs a script defined in the scripts section of package.json . |
View or set config variables | pip config get/set | npm config get/set | Gets or sets configuration variables. |
Virtual Environments
Task | pip Command | npm Command | Description |
---|
Create a virtual environment | python -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 environment | source <env_name>/bin/activate (Linux/macOS) or <env_name>\Scripts\activate (Windows) | N/A | Activates a Python virtual environment. NPM does not have a direct equivalent. |
Cleaning Up
Task | pip Command | npm Command | Description |
---|
Clean up cache | pip cache purge | npm cache clean --force | Cleans the local cache of downloaded files. |
Lock Files
Task | pip Command | npm Command | Description |
---|
Generate a lock file | N/A (pip does not natively support lock files) | npm shrinkwrap or npm install --package-lock | Generates a package-lock.json file that locks the versions of installed dependencies. |
Environment Variables
Task | pip Command | npm Command | Description |
---|
List environment variables | pip config list | npm config list | Lists environment variables related to pip or npm configuration. |
Set environment variable | pip 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).