DEV Community

Denys Potapov
Denys Potapov

Posted on

2

npm hack to remove unused transitive dependencies

Image description

Unused dependencies can be a pain in javascript project. For example your use foo package in your project. And foo package depends on bar package. Sometimes code from bar is unused in your project, but there is no way to remove it.

I've created an empty dry-uninstall package to «solve» an issue with unused transitive dependencies:

{
  "overrides": {
    "foo": {
      "bar": "npm:dry-uninstall"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

It uses hack in overrides section of package.json to replace bar dependency of foo with an empty index.js:

module.exports = {};
Enter fullscreen mode Exit fullscreen mode

Reasoning

The main reasons why this could help are:

  1. Bundle size. When tree-shaking does not work for some reasons.

  2. Simplified Build. There was a long time when installing mongodb required kerberos package. That itself required to build native add-on modules, and it can be painful.

  3. Security. Replace the vulnerable packages, to be sure that the code is never used.

  4. License Issues.

Sample: Removing momentjs package from simple-node-logger

You've created a minimal project that uses simple-node-logger, simple multi-level logger for console and files:

// index.js
const SimpleLogger = require('simple-node-logger');
const appender = new SimpleLogger.appenders.ConsoleAppender();

const manager = new SimpleLogger();
manager.addAppender(appender);

const log = manager.createLogger();
log.info('this is a simplelog statement');
Enter fullscreen mode Exit fullscreen mode

Check the size of bundle (~383 KB)

    esbuild index.js --bundle --platform=node --outfile=index.dist.js

      index.dist.js  383.7kb
Enter fullscreen mode Exit fullscreen mode

Removing momentjs

Update timestamp format code to remove the momentjs dependency:

// index.js
appender.formatter = function(entry) {
    const fields = this.formatEntry( entry, appender);

    return fields.join( appender.separator );
};
appender.formatTimestamp = (ts) => {
    return ts.toString();
};
Enter fullscreen mode Exit fullscreen mode

Override momentjs with dry-uninstall in package.json

{
  "dependencies": {
    "simple-node-logger": "^21.8.12"
  },
  "overrides": {
    "moment": "npm:dry-uninstall@0.3.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Check the size of bundle (~241 KB)

    esbuild index.js --bundle --platform=node --outfile=index.dist.js

      index.dist.js  241.7kb
Enter fullscreen mode Exit fullscreen mode

Saved around 140 kb, not bad.

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay