DEV Community

Aviskar KC
Aviskar KC

Posted on

.npmignore: A small step towards unbloating JS

Javascript bloat is a real thing. There are blog posts out there that help you migrate from a heavy library to a much smaller one while tools like bundlephobia help you calculate the cost of adding a package to your application.

As library maintainers, you can make use of .npmignore to ignore unnecessary files from being included in your package which will in turn help reduce the amount of code that gets shipped to projects that use your library.

Some of the things you can keep out of your package are:

1) If your package only requires the final build file to work, you can keep out the src folder.
2) The tests folder.
3) The files containing demos/examples of how your library works.
4) Your dotfiles that aren't already in .gitignore like eslint/prettier configs.

Ignoring these files, helped me bring down my library, react-d3-donut's size (which was previously around 1MB) down to just 138B.

The .npmignore file works exactly like .gitignore in terms of syntax and how you would whitelist/blacklist file. You can find .npmignore's documentation here:
https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

Top comments (2)

Collapse
 
dezren profile image
dezren

thanks for the article! I found it while looking into this topic. Some other articles did suggest whitelisting in the 'files' property of package.json or to just use .gitignore so that it always matches. I can still see times you'd want to use .npmignore too though!

Collapse
 
aviskarkc10 profile image
Aviskar KC

I am glad you found it useful. Yeah some files you want to push to remote but not publish it. Have to be careful though: medium.com/@jdxcode/for-the-love-o...