LinuxCommandLibrary

npm-uninstall

Remove installed npm packages

TLDR

Remove a package from the current project

$ npm [[r|uninstall]] [package_name]
copy

Remove a package globally
$ npm [[r|uninstall]] [[-g|--global]] [package_name]
copy

Remove multiple packages at once
$ npm [[r|uninstall]] [package_name1 package_name2 ...]
copy

SYNOPSIS

npm uninstall <package-name>
npm uninstall <package-name>@<version>
npm uninstall -g <package-name>
npm uninstall <package-name> --save-dev
npm un <package-name> (alias)
npm rm <package-name> (alias)

PARAMETERS

-g, --global
    Removes the package from the global installation directory.

--save-dev, -D
    Removes the package from 'devDependencies' in package.json.

--save-optional, -O
    Removes the package from 'optionalDependencies' in package.json.

--no-save
    Prevents updating 'package.json' and 'package-lock.json'.

--dry-run
    Simulates the uninstall process without making any actual changes.

--ignore-scripts
    Prevents preuninstall and postuninstall lifecycle scripts from running.

--force
    Forcefully removes a package, even if it is a dependency of another installed package. Use with extreme caution.

DESCRIPTION

npm uninstall is a fundamental command used to remove installed Node.js packages from your project's node_modules directory, or globally from your system. When executed, it meticulously unlinks and deletes the specified package and its associated files.

For local project installations, it automatically updates your package.json file by removing the entry from the dependencies, devDependencies, or optionalDependencies section, and also updates the package-lock.json to reflect the new dependency tree. This ensures consistency and reproducibility of your project's environment. The command is crucial for managing project size, removing unused libraries, resolving dependency conflicts, or reverting unwanted installations. It efficiently cleans up your environment, helping maintain a tidy and performant development setup.

CAVEATS

Permission Issues: Global uninstalls often require elevated privileges (e.g., sudo on Linux/macOS, Administrator on Windows).
Broken Dependencies: Using --force can remove packages that other installed packages depend on, potentially breaking your project or other global tools.
Leftover Data: While npm uninstall removes the package, it doesn't clean npm's cache. Use npm cache clean --force to clear it.
Dependency Resolution: npm uninstall primarily removes the specified package. It doesn't automatically remove its own dependencies unless they are no longer required by any other package in the tree (though npm prune can help with extraneous packages).

ALIASES

The npm uninstall command can also be invoked using its shorter aliases: npm un and npm rm.

LOCAL VS. GLOBAL

By default, npm uninstall operates on packages installed locally within the current project's node_modules directory. To remove a package installed globally, you must use the -g or --global flag.

HISTORY

The npm uninstall command has been a core part of the npm CLI since its early versions, evolving alongside the Node.js ecosystem. Initially, managing package.json dependencies required explicit --save flags. However, with npm@5 and later, the default behavior for npm install became to automatically save to dependencies, and similarly, npm uninstall now defaults to removing entries from package.json and updating package-lock.json. This automation streamlined dependency management, making it more intuitive and reducing boilerplate. The introduction of package-lock.json further enhanced determinism in installations and uninstalls, ensuring consistent dependency trees across environments. Its functionality has remained steadfast, focusing on reliable package removal.

SEE ALSO

npm install(1), npm update(1), npm prune(1), npm ls(1), npm cache(1)

Copied to clipboard