NPM installation commands and version ranges

Learn to use npm install, npm uninstall, and npm update, and understand semantic version ranges and lockfiles.

🎯 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.
For production, specify dependency versions explicitly to avoid automatically upgrading to incompatible releases.

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
FlagFull formDestination
NoneNonedependencies
-D--save-devdevDependencies
-O--save-optionaloptionalDependencies
-g--globalGlobal 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
Globally installed packages are not recorded in the project's 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:

SyntaxMeaningExample
1.2.3Exact versionInstall only 1.2.3
^1.2.3Latest compatible 1.x releaseAllows 1.9.0, but not 2.0.0
~1.2.3Latest compatible 1.2.x releaseAllows 1.2.9, but not 1.3.0
>=1.2.3Version 1.2.3 or laterAllows higher versions
*Any versionNot recommended; may introduce breaking changes

Combine version range styles

package.json
{
    "dependencies": {
        "vue": "^3.5.0",
        "lodash": "~4.17.21",
        "dayjs": "1.11.13"
    }
}
Avoid * 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

  1. In your npm-demo project, install axios@1.7.9 into dependencies:
# Enter your command here
  1. Install nodemon into devDependencies:
# Enter your command here
  1. Run npm list --depth=0 and inspect the direct dependencies.
  2. Try removing nodemon and confirm that it is no longer in package.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.