Terminus
Removing npm Packages

Removing npm Packages

The short answer

To remove locally installed packages, you can use the [.inline-code]npm uninstall[.inline-code] command from your project’s directory:

$ npm uninstall <package_name …>

Where:

  • [.inline-code]package_name[.inline-code] is the name(s) of the package(s) you want to remove from the project.

This command will automatically remove the packages located in the [.inline-code]node_modules[.inline-code] directory and update the [.inline-code]dependencies[.inline-code], [.inline-code]devDependencies[.inline-code], [.inline-code]optionalDependencies[.inline-code], and [.inline-code]peerDependencies[.inline-code] objects in the [.inline-code]package.json[.inline-code] file to reflect those changes.

[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-with-ai]

If you're using Warp as your terminal, you can easily recall npm commands using the Warp AI Command Suggestions feature.

Entering [.inline-code]npm uninstall local package[.inline-code] in the AI Command Suggestions will prompt an [.inline-code]npm uninstall[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#remove-scoped-packages] Removing scoped packages [#remove-scoped-packages]

To remove a scoped package, you can use the following syntax:

$ npm uninstall @<scope>/<package_name>

Where:

  • [.inline-code]@scope[.inline-code] is the namespace of the user or organization this package belongs to.

For example:

$ npm uninstall lodash
$ npm uninstall @angular/core

[#remove-global-packages] Removing global packages with the [.inline-code]-g[.inline-code] flag [#remove-global-packages]

A global package is a package that's installed system-wide on your machine, eliminating the need for frequent reinstallation.

To remove a global package, you can use the [.inline-code]npm uninstall[.inline-code] command with the [.inline-code]-g[.inline-code] flag as follows:

$ npm uninstall -g <package_name>
$ npm uninstall -g @<scope>/<package_name>

For example:

$ npm uninstall -g jshint
$ npm uninstall -g @babel/core

[#remove-unused-dependencies] Cleaning unused dependencies [#remove-unused-dependencies]

After removing packages, your project's [.inline-code]node_modules[.inline-code] directory may still contain extraneous dependencies that were once required by these packages. Although not explicitly listed as dependencies in the [.inline-code]package.json[.inline-code] file, they usually remain in your project's file structure, and need to be periodically cleaned up in order to reclaim disk space.

To remove these packages and ensure that your project only contains necessary dependencies, you can use the [.inline-code]npm prune[.inline-code] command as follows:

$ npm prune

[#remove-packages-with-no-save] Removing packages without updating the [.inline-code]package.json[.inline-code] file [#remove-packages-with-no-save]

When you remove a package, it's important that this change is accurately reflected in the [.inline-code]package.json[.inline-code] and [.inline-code]package-lock.json[.inline-code] files. However, there might be instances when you want to uninstall a package without modifying this metadata. These scenarios often involve temporary adjustments, debugging, or environment-specific requirements.

For instance, when you're debugging a build issue caused by a specific package and want to remove it temporarily for testing. Or when you're collaborating on a project with other developers and need to keep your adjustments confined to your local environment in order not to corrupt the shared configuration.

To remove a package without altering the [.inline-code]package.json[.inline-code] and [.inline-code]package-lock.json[.inline-code] files, you can use the [.inline-code]--no-save[.inline-code] flag as follows:

$ npm uninstall --no-save <package_name>

For example:

$ npm uninstall --no-save @angular/core

If you want to learn more about cached packages and further optimize your package management, you can read our article on how to clear the npm cache.

[#confirm-package-uninstallation] Confirming package uninstallation [#confirm-package-uninstallation]

After running the [.inline-code]npm uninstall[.inline-code] command, it's essential to verify the successful removal of the package as it helps to maintain the integrity and reliability of your project and reduces potential conflicts between packages.

To get a list of the locally installed packages present in your [.inline-code]node_modules[.inline-code] folder, you can use the [.inline-code]npm ls[.inline-code] command from your project's directory as follows:

$ npm ls

Alternatively, you can use the [.inline-code]npm ls[.inline-code] command with the [.inline-code]-g[.inline-code] flag to get a list of the globally installed packages as follows:

$ npm list -g

If you want to learn more about listing dependencies, you can read our article on how to list locally and globally installed packages with npm.

[#remove-packages-from-the-npm-registry] Unpublishing packages from the registry [#remove-packages-from-the-npm-registry]

Before removing a package from the registry, make sure you're logged in to your npm account. You can log in using the following command and providing your credentials:

$ npm login

To remove a specific package version from the registry, you can use the [.inline-code]npm unpublish[.inline-code] command as follows:

$ npm unpublish <package_name>@<package_version>

For example:

$ npm unpublish lodash@4.17.21

After unpublishing, you can run [.inline-code]npm cache clean[.inline-code] to clear the npm cache of references to the removed package:

$ npm cache clean -f