7

Need a solution to remove unused node modules and node modules dependencies from a project.

I have searched internet and found couple of NPM package to do something similar but not exactly my requirement.

Please note, Node modules should be listed/removed if that is neither a dependencies in other node modules nor used in any project code.

Please help

sachin kumar
  • 159
  • 1
  • 1
  • 9

3 Answers3

8

Perhaps you could use a tool like depcheck:

Depcheck is a tool for analyzing the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are missing from package.json.

Another one is npm-check

Check for outdated, incorrect, and unused dependencies. ... Kindly informs you if a dependency is not being used in your code ...

jacobq
  • 11,209
  • 4
  • 40
  • 71
  • Tried npm-check, it uses a huge amount of my ram and swap and my pc freezes shortly after. depcheck seems to work properly. – Bogdan Aug 19 '21 at 11:07
2

Run npm prune to unbuild modules not listed in package.json.

From npm help prune:

This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.

Extraneous packages are packages that are not listed on the parent package's dependencies list.

If the --production flag is specified, this command will remove the packages specified in your devDependencies.

From : npm command to uninstall unused packages in Node.js

Léo R.
  • 2,620
  • 1
  • 10
  • 22
1

I've recently used npm-check

Check for outdated, incorrect, and unused dependencies.

After installing just run the command in terminal:

$ npm-check

npm check result

Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
  • doesn't work correctly with react/remix set up. Gave me many unused packages and I'm 100% that they are used. – Mo Zaatar Feb 21 '22 at 00:31