Guide · Lean software
Less code = more: how to reduce npm dependencies
Every package you install is code you didn't write, didn't review and now have to trust. With AI assistants adding packages faster than ever, keeping dependencies lean is one of the cheapest security and performance wins available.
Why this matters more in the AI era
A typical modern app pulls in hundreds of transitive packages. Each one can be abandoned, broken by an upgrade, or hijacked in a supply-chain attack. AI coding assistants make this worse: they add a package for a five-line task without hesitation.
My rule, long before AI: zero dependencies is the aspiration, not the constraint. Every package has to earn its place.
Step 1: See what you actually have
# full dependency tree (direct + transitive)
npm ls --all
# why is this package here?
npm explain lodash
# flag packages your code never imports (one-off, no install)
npx depcheckCheck the size of anything new before adding it. Tools like Bundlephobia (browser) and Packagephobia (install size) make the cost visible.
Step 2: Replace packages with the platform
Node.js and browsers now ship much of what we used to install:
| Instead of | Use | Available in |
|---|---|---|
node-fetch, axios | fetch() | Node 18+, browsers |
uuid | crypto.randomUUID() | Node 19+ (global), browsers |
dotenv | node --env-file=.env | Node 20.6+ |
nodemon | node --watch | Node 18.11+ |
rimraf | fs.rm(path, { recursive: true }) | Node 14.14+ |
lodash.clonedeep | structuredClone() | Node 17+, browsers |
moment | Intl.DateTimeFormat | Node, browsers |
Step 3: Using one function from a big library? Port it
A common pattern: a project installs a whole utility library and uses one or two functions from it. In that case, the better move is often to port just what you use into your own code. You get one less package to update and audit, and code you actually understand.
# every line that imports or requires the package
grep -rn "lodash" src/
# which functions? (e.g. import { debounce } from "lodash")
grep -rhoE "import [{][^}]*[}] from .lodash" src/If the answer is "just debounce", that's ten lines:
// Replaces our lodash dependency: debounce was the only function we used.
export function debounce(fn, wait = 200) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), wait);
};
}- Check the license. MIT, BSD and Apache allow copying, but you must keep the copyright and license notice. A comment with the source and license is enough.
- Port only what you need. You don't need every option and edge case the library supports, only the behaviour your app relies on.
- Add tests for that behaviour before removing the package, so the swap is provably safe.
- Then uninstall and check the bundle size and lockfile shrink.
When not to port: security-sensitive code (cryptography, authentication, parsers of untrusted input) and anything that changes often. There you want the maintainers' fixes, not a frozen copy.
Step 4: Put guardrails in place
- Commit the lockfile and install with
npm ciin CI, so builds use exactly what you reviewed. - Review new dependencies in pull requests like code: who maintains it, how big is it, what does it pull in?
- Automate updates with Renovate or Dependabot, in small batches you can actually review.
- Be careful with install scripts: they run arbitrary code on your machine and in CI.
When a dependency is worth it
Lean doesn't mean reinventing everything. Cryptography, parsers, database drivers and complex domains (dates across time zones, PDFs, payments) are exactly where a well-maintained library beats home-made code. The goal is intentional dependencies, not zero at any cost.
Smaller bundles also mean faster serverless cold starts. More on that in the AWS Lambda cost guide.
FAQ
Why reduce npm dependencies?
Fewer dependencies mean a smaller attack surface for supply-chain attacks, fewer breaking upgrades, faster installs and builds, smaller bundles and faster cold starts on serverless platforms like AWS Lambda.
How do I find unused dependencies?
Start with npm ls --all to see the full tree and npm explain <package> to see why something is installed. A one-off run of a tool such as depcheck flags packages your code never imports.
Which packages can be replaced with native Node.js APIs?
Common ones: node-fetch and axios (global fetch), uuid (crypto.randomUUID), dotenv (node --env-file), nodemon (node --watch), rimraf (fs.rm with recursive), and many lodash helpers (modern array and object methods, structuredClone).
Should I copy a function from an npm package into my project?
If you only use one or two small functions from a large library, porting them into your own codebase is often the better trade: one less dependency to update and audit, and code you fully understand. Check the license first (MIT and similar licenses require keeping the copyright notice), add tests, and keep a comment pointing to the original. Don't do it for security-sensitive code like cryptography or parsers, where you want the maintainers' fixes.
Does AI-generated code add more dependencies?
Often, yes. Assistants tend to reach for a package for small tasks the platform already handles, and occasionally suggest packages that don't exist, which attackers can register. Review every new dependency in pull requests, whoever wrote the code.