When listing npm packages, you may see an error with code extraneous
as shown below:
$ npm list --depth=0
[email protected] /Desktop/DEV/n-app
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected]
# ...
npm ERR! extraneous: [email protected] /DEV/n-app/node_modules/arr-diff
npm ERR! extraneous: [email protected] /DEV/n-app/node_modules/arr-union
npm ERR! extraneous: [email protected] /DEV/n-app/node_modules/arr-flatten
npm ERR! extraneous: [email protected] /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! 👍