Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commonjs unused module.exports detected vs exports not detected #580

Closed
hugomallet opened this issue Apr 4, 2024 · 4 comments
Closed

commonjs unused module.exports detected vs exports not detected #580

hugomallet opened this issue Apr 4, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@hugomallet
Copy link

hugomallet commented Apr 4, 2024

Hi,

Unused commonjs exports are not detected when they are exported using the exports alias.

For example, this unused export would be detected :

module.exports.add = () => {};

while this one won't be :

exports.add = () => {};

Thanks

@hugomallet hugomallet added the bug Something isn't working label Apr 4, 2024
@webpro
Copy link
Collaborator

webpro commented Apr 4, 2024

That's correct, also see https://knip.dev/guides/commonjs for recommendations.

This is another subtle caveat with the dynamic nature of CommonJS. Using module.exports is generally preferred over exports (also see e.g. https://www.hacksparrow.com/nodejs/exports-vs-module-exports.html).

Not sure if Knip should still try to support this, but at least we can update the docs.

@hugomallet
Copy link
Author

Thanks for the link. The best is to avoid reassigning module.exports as much as possible, because anyway any previous export will be lost.

That would be great if Knip could support this. It would avoid refactoring every exports of big codebases before cleaning up unused one :)

@webpro webpro closed this as completed in 404236b Apr 5, 2024
@webpro
Copy link
Collaborator

webpro commented Apr 5, 2024

🚀 This issue has been resolved in v5.8.0. See Release 5.8.0 for release notes.

Using Knip in a commercial project? Please consider sponsoring me.

@webpro
Copy link
Collaborator

webpro commented Apr 5, 2024

Alright then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants