Understanding npm ERR! code extraneous with your npm dependencies

Posted on Jun 10, 2022

Learn about npm ERR! code extraneous and how to resolve it

When listing npm packages, you may see an error with code extraneous as shown below:

$ npm list --depth=0
n-app@1.0.0 /Desktop/DEV/n-app
├── acorn@5.7.4
├── acorn-dynamic-import@2.0.2
├── ajv@4.11.8
├── ajv-keywords@1.5.1
├── align-text@0.1.4
├── ansi-regex@2.1.1
├── anymatch@3.1.2
├── arr-diff@4.0.0 extraneous
├── arr-flatten@1.1.0 extraneous
├── arr-union@3.1.0 extraneous
├── array-unique@0.3.2 extraneous
├── asn1.js@5.4.1
# ...

npm ERR! extraneous: arr-diff@4.0.0 /DEV/n-app/node_modules/arr-diff
npm ERR! extraneous: arr-union@3.1.0 /DEV/n-app/node_modules/arr-union
npm ERR! extraneous: arr-flatten@1.1.0 /DEV/n-app/node_modules/arr-flatten
npm ERR! extraneous: array-unique@0.3.2 /DEV/n-app/node_modules/array-unique

The npm error extraneous means that there are installed packages in your node_modules/ folder that is not listed on your package.json file.

The npm list command looks into your package.json file and the node_modules/ folder to list the packages installed there.

When a package is not listed in the package.json file, it will be marked as extraneous.

In npm version 4 and below, you need to add the --save option when running npm install to save it in the package.json file. This is why it’s common to see a package marked as extraneous in the past.

Since npm version 5, the npm install command will automatically list the package in the package.json file without adding the --save option.

To resolve extraneous errors in the npm list output, you need to add the packages in your package.json file.

You need to make sure that you’re using the latest version of npm first:

npm install -g npm@latest

Once the upgrade is done, run the npm list command again to see if the extraneous error is still there.

If you see the error, remove all packages installed in your node_modules/ folder:

rm -rf node_modules

Then run the npm install command to install the dependencies again.

For every package that’s excluded from your dependencies object, run the npm install command to get them in the package.json list.

And that’s how you resolve the extraneous errors in npm. Nice work! 👍

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.