Removing extraneous NPM package in JavaScript and Node.js

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
3 min

banner

# Removing extraneous NPM package in JavaScript and Node.js

When you list the installed packages in your project, you might get an error "npm ERR! extraneous" or you might see the label "extraneous" next to package names.

npm err extraneous

shell
npm ERR! extraneous: accepts@1.3.8 npm ERR! extraneous: array-flatten@1.1.1 npm ERR! not ok code 0

The issue occurs when a package is installed but is not listed in your project's package.json file.

The npm list command prints the installed packages and their versions based on the contents of your node_modules directory.

When there is a mismatch between your node_modules directory and the dependencies in your package.json file, the extraneous label is shown.

You can resolve the issue by using the npm prune command.

shell
npm prune

remove extraneous packages with npm prune

The npm prune command removes extraneous packages.

These are the packages that are present in your node_modules directory that are not listed in your package.json file.

You can issue the npm list command to verify that the unnecessary packages have been removed.

shell
npm list

npm list after removing extraneous

# Delete your node_modules and reinstall your dependencies

If the issue persists, delete your node_modules and package-lock.json files.

The following command deletes the files on Windows.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock

The following command deletes the files on macOS and Linux.

shell
# ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock

Run the following command to clean your npm cache.

shell
# ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force

And install the packages that are present in your package.json file.

shell
npm install

# Update your version of npm

If the issue persists, try updating your npm version.

shell
npm install -g npm@latest # ๐Ÿ‘‡๏ธ If you get a permissions error on macOS / Linux sudo npm install -g npm@latest

update npm version

If you get a permissions error on Windows, open CMD as an administrator and rerun the command.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Rerun the command.
shell
npm install -g npm@latest npm install -g npm@latest --force

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev