Working with CommonJS
CommonJS is the JavaScript module system using require()
and module.exports
statements.
Knip works well with CommonJS. You don’t need to use ES Modules or a
tsconfig.json
to use Knip. But due to the dynamic nature of CommonJS, we’ll
have to agree on a few conventions. Those conventions are designed to minimize
impact on existing codebases, improve consistency, and ease migration to ES
Modules or TypeScript.
For named exports, the recommendation is to assign keys to module.exports
:
Alternatively, assign an object with ONLY shorthand property assignments to
module.exports
:
Anything else assigned to module.exports
is considered a default export, and
should be imported as such.
The following default import of the named exports above will result in all those exports reported as unused, even when referenced like below:
Instead, do this:
Not recommended per se, but the following import syntax also results in the
named export A
being used:
Add a non-shorthand property to turn the named object notation into a single default export:
ISC License © 2024 Lars Kappert