NPM installation commands and version ranges
🎯 Introduction
In this article, you will learn NPM's most common installation, removal, and update commands, understand the ^ and ~ prefixes in version ranges, and choose a version management strategy for your project.
🧱 Basic npm install usage
npm install is NPM's most frequently used command for installing project dependencies. Here are its common forms:
Install all project dependencies
After cloning a project, your first step is usually:
npm install
NPM reads dependencies and devDependencies in package.json and downloads the dependencies into node_modules.
Install a specific package
npm install dayjs
npm install dayjs@1.11.13
npm install dayjs@latest
- No version specified: install the latest version that satisfies the requirements in
package.json. @version: install a specific version.@latest: install the latest published version.
Install into different dependency fields
npm install axios # Add to dependencies
npm install eslint -D # Add to devDependencies
npm install typescript -O # Add to optionalDependencies
| Flag | Full form | Destination |
|---|---|---|
| None | None | dependencies |
-D | --save-dev | devDependencies |
-O | --save-optional | optionalDependencies |
-g | --global | Global installation; not added to package.json |
⚡ Global installation
Some tools, such as http-server and create-vite, may be useful from any directory rather than just one project:
npm install http-server -g
npm install create-vite -g
Once installed globally, you can run the command from any directory:
http-server
package.json. For team projects, prefer devDependencies so everyone uses the same version.🗑 Remove and update dependencies
Remove a dependency
npm uninstall dayjs
NPM removes the package from node_modules and its entry from package.json.
Update dependencies
npm update dayjs
npm update
npm update dayjs: update only the specified package.npm update: update all packages within their allowed version ranges.
npm update stays within the ranges in package.json. For example, ^1.11.13 allows compatible 1.x.x releases, but not 2.0.0.⚖️ Understanding version ranges
Versions in package.json often include special symbols. Common forms include:
| Syntax | Meaning | Example |
|---|---|---|
1.2.3 | Exact version | Install only 1.2.3 |
^1.2.3 | Latest compatible 1.x release | Allows 1.9.0, but not 2.0.0 |
~1.2.3 | Latest compatible 1.2.x release | Allows 1.2.9, but not 1.3.0 |
>=1.2.3 | Version 1.2.3 or later | Allows higher versions |
* | Any version | Not recommended; may introduce breaking changes |
Combine version range styles
{
"dependencies": {
"vue": "^3.5.0",
"lodash": "~4.17.21",
"dayjs": "1.11.13"
}
}
* or unspecified versions where possible. Different installations may resolve to different versions, leading to “it works on my machine” problems.💡 Check installed versions
To see which versions are installed, run:
npm list
npm list --depth=0
--depth=0 lists only direct project dependencies, leaving out their dependencies for a clearer view.
❓ Questions and answers
Q1: How do npm install and npm install package-name differ?
npm install installs all dependencies listed in package.json. npm install package-name installs the specified package and adds it to dependencies.
Q2: Which is stricter, ^ or ~?
~ is stricter. ~1.2.3 allows patch updates only, while ^1.2.3 also allows minor updates.
Q3: How do global and local installations differ?
A global installation makes commands available from any directory but is not recorded in package.json. A local installation belongs to the current project and can be recorded in package.json for team collaboration.
Q4: Why is npm install sometimes slow?
NPM downloads from its official registry by default, which can be slow to access from mainland China. We will learn how to switch to a mirror in China to speed up downloads.
🧪 Practice
- In your
npm-demoproject, installaxios@1.7.9intodependencies:
# Enter your command here
- Install
nodemonintodevDependencies:
# Enter your command here
- Run
npm list --depth=0and inspect the direct dependencies. - Try removing
nodemonand confirm that it is no longer inpackage.json.
🎉 You now know how to install, remove, and update NPM packages and work with version ranges! Next, we will explore npm scripts and npx to streamline command-line tasks.
