Declaring Dependencies in a Library: Practices and Their Anti-Patterns
An application can pin every dependency exactly, because it controls the whole install. A library cannot. Its dependency declarations are merged with everyone else's in the consumer's project, and the resolver has to find versions that satisfy all of them. That changes what good practice looks like. Each item below gives the practice, why it helps, and the habit it replaces.
1. Use compatible ranges, not exact pins
Do: declare "^2.3.0" in package.json, "requests>=2.28,<3" in pyproject.toml, or the default caret form in Cargo.toml.
Why: a range lets the consumer's resolver pick one shared copy that satisfies every package in the tree, and lets them receive security patches without waiting for you.
Anti-pattern: exact pins such as "2.3.0" in a library. In npm they cause duplicate copies in node_modules; in Python they cause unsolvable conflicts the moment another package needs a different patch release.
2. Set the lower bound to what you actually test
Do: make the minimum version the oldest one your CI exercises, and run at least one job that installs the lowest allowed versions. Some tools support this directly, such as uv's lowest resolution mode, or Cargo's unstable -Z minimal-versions flag on nightly.
Why: a lower bound is a promise that older versions work.
Anti-pattern: copying whatever version was current when you added the dependency and never checking whether you rely on a newer feature.
3. Be cautious with upper bounds in Python
Do: add an upper bound when you know a future major will break you, or when the dependency documents breaking changes at each major.
Why: pip has no way to override a library's bounds, so an unnecessary cap blocks users from upgrading something unrelated to you.
Anti-pattern: capping every dependency below the next major by reflex, then never relaxing the caps.
4. Move host frameworks to peer dependencies
Do: if your package is a plugin for a framework, list the framework in peerDependencies with the range of majors you support.
"peerDependencies": {
"example-framework": "^4.0.0 || ^5.0.0"
},
"devDependencies": {
"example-framework": "^5.2.0"
}
Why: the user's application should own the framework version; you only state compatibility.
Anti-pattern: putting the framework in dependencies, which can install a second copy and break instance checks or shared state.
5. Do not ship a lockfile as a constraint
Do: commit a lockfile for reproducible development and CI if you like, but remember that consumers ignore it.
Why: npm does not use a dependency's package-lock.json, and Python installers do not read a library's lock file. Only your declared ranges reach users.
Anti-pattern: assuming a passing CI run with a lockfile proves the declared ranges work.
6. Keep the dependency list short
Do: question each runtime dependency, especially tiny ones, and move build or test tools to dev dependencies.
Why: every entry is more install time, more licences to review and more supply-chain exposure for your users.
Anti-pattern: leaving a bundler or test runner in dependencies by accident.
More on this: peer dependencies explained and lockfiles in libraries.