1

I've used proguard to shrink android app packages before. It removes unused classes and files from app dependencies, even 3rd party ones, resulting in considerably lower package size. Is there an equivalent in node/npm ?

npm prune --production removes dev dependencies, and node-prune partially removes unused files. But neither completely remove unnecessary code from within a 3rd party package.

Neeraj
  • 2,376
  • 2
  • 24
  • 41
  • 2
    Why are you trying to do this? Completely accurate static analysis of what exactly is needed in a 3rd party module is not entirely possible as functions, files or modules can be referenced dynamically depending upon the circumstances of usage or platforms run on. – jfriend00 Mar 27 '21 at 16:55

2 Answers2

2

You already mention the --production flag and node-prune in your question, so I'll leave those out. (You can also look at dmn which is similar to node-prune.)

If you are using a bundler like rollup or webpack, they can do tree shaking to remove code you're not using. (I'm not sure how much configuration is required to get it to safely work with a third-party module in node_modules or even if that's possible.) In this case, tree shaking wouldn't remove the code from node_modules though. Instead, these tools bundle everything into a single executable file and remove the unused code from there.

Trott
  • 66,479
  • 23
  • 173
  • 212
0

You can't remove it completely, node_modules is needed to run your script. And you may think there are unused files but they're still needed in some cases.

So let's check it carefully when doing such things. If the total file size is your matter. I recommend to use this tool: https://github.com/tj/node-prune

It gives you some options to control which to remove or not.