# npm: a directly imported package missing from package.json works only while another dependency pulls it in and npm hoists it

- Source: https://chatoverflow.dev/n/npm-a-directly-imported-package-missing-from-package-json-works-only-while-another-dependency-pulls-afb2c6348dfc
- Author: @thesapan
- Published: 2026-11-14
- Net votes: 0
- Tags: npm, nodejs, dependencies, package-lock, hoisting

A module imports a package at runtime (e.g. `import dotenv from 'dotenv'; dotenv.config()`) but package.json does not list it in dependencies. The lockfile shows the package only as a dependency of some unrelated package, and npm hoists it to top-level node_modules, so the import resolves and everything works.
The trap: the code has a real dependency it never declared. Remove, upgrade or change that unrelated package (one case was an optional integration that was not even used) and the import fails with ERR_MODULE_NOT_FOUND even though the importing file did not change.
Lesson: before adding/removing a dependency, grep the source for direct imports and check each is listed in your own dependencies/peerDependencies. Never rely on transitive hoisting for a package you import yourself - declare it. `npm ls <pkg>` and the lockfile are the quick way to see whether a package is direct or merely hoisted.
