-
Notifications
You must be signed in to change notification settings - Fork 0
Using the NPM Command Line
The NPM (Node Package Manager) command line interface (CLI) is an essential tool for managing your project’s dependencies and performing various tasks in your JavaScript development environment. In this guide, you’ll learn how to use the NPM CLI effectively to install, update, manage, and even automate your project tasks. This is a professional guide aimed at helping you master NPM from the basics to advanced uses.
To use NPM commands, you first need to open the Command Line Interface (CLI), which can be:
- Windows: Command Prompt or PowerShell
- macOS: Terminal
- Linux: Terminal (depending on your distribution)
Icon for CLI:
- 🖥️ Command Prompt (Windows)
- 💻 Terminal (macOS/Linux)
NPM commands follow a basic syntax:
npm <command> <package-name> <options>Where:
-
<command>: The action you want to perform (e.g.,install,uninstall,update). -
<package-name>: The name of the package you're working with (e.g.,lodash). -
<options>: Optional flags or parameters (e.g.,--save,--global).
Let’s walk through some of the most commonly used NPM commands and how to use them effectively with plenty of examples.
To install a package, use the npm install command.
Basic Installation (Locally):
npm install <package-name>Example:
npm install lodashThis installs the Lodash library and saves it into your node_modules folder (local to your project).
Global Installation:
If you want to install a package globally (making it accessible anywhere on your system), use the -g flag:
npm install -g <package-name>Example:
npm install -g typescriptThis installs TypeScript globally, allowing you to use it anywhere on your computer.
To check which packages are installed in your project:
npm listThis will list all the locally installed packages in your project.
Example Output:
my-project@1.0.0 /path/to/your/project
├── lodash@4.17.21
└── express@4.17.1Global Packages: To view globally installed packages, use:
npm list -g --depth=0This command will show only the globally installed packages without listing their dependencies.
To remove a package from your project, use the npm uninstall command:
npm uninstall <package-name>Example:
npm uninstall lodashThis removes Lodash from your project and updates the package.json file.
Uninstall Globally Installed Packages:
To remove a globally installed package, use the -g flag:
npm uninstall -g typescriptTo update a package to its latest version, use the npm update command.
npm update <package-name>Example:
npm update lodashThis will update Lodash to the latest version based on the version range defined in your package.json.
To update all packages in your project:
npm updateYou can install a specific version of a package by specifying the version number.
npm install <package-name>@<version>Example:
npm install lodash@4.17.19This installs Lodash version 4.17.19.
The package.json file is essential for tracking your project’s dependencies. When you install a package, NPM will add it to your package.json by default.
Saving Dependencies:
By default, packages are saved in the dependencies section of your package.json.
If you want to install a package only for development purposes (i.e., not in production), use the --save-dev flag:
npm install --save-dev eslintThis installs ESLint and adds it to the devDependencies in your package.json.
Icon for dependencies:
- 🛠️ Development Dependencies (e.g., Testing Tools)
- 🚀 Production Dependencies (e.g., Frameworks, Libraries)
To start a new Node.js project, use the npm init command to create a package.json file. You can use the -y flag to skip the prompts and generate it automatically with default settings.
npm initor
npm init -yThis creates a package.json file in your project directory.
You can define custom scripts in the package.json file that can be run using NPM.
Example Script:
In your package.json, you can define a script like this:
"scripts": {
"start": "node index.js",
"test": "jest"
}To run the script, use:
npm run startTo run the test script:
npm run testNote: The npm run command is used for custom scripts, but npm start is a shorthand for running the start script.
NPM also helps you check for known security vulnerabilities in the installed packages. Run this command:
npm auditThis will show any vulnerabilities in the packages you’re using, along with recommendations for fixing them.
NPM provides several useful flags and options to customize the behavior of commands. Here are some common flags:
-
-g: Install packages globally (accessible anywhere on your computer). -
--save-dev: Install a package as a development dependency (added todevDependencies). -
--save: Automatically adds the package to yourdependencies(this is the default). -
--global: Equivalent to-g, installs globally. -
--silent: Suppresses output from the command, making it run quietly. -
--dry-run: Show what changes would be made without actually installing or updating anything.
npx is a command that comes with NPM and allows you to execute binaries from NPM packages without installing them globally.
For example, to run the create-react-app tool without installing it globally, you can run:
npx create-react-app my-appIf you need to manage multiple versions of Node.js (for example, for different projects), you can use NVM (Node Version Manager). Install NVM, and use the following commands to switch between Node.js versions:
nvm install 14
nvm use 14