Learn to use .npmignore

原文链接: caurea.org

The .npmignore file does not work like .gitignore. And many package maintainers have their packages misconfigured. Why do I care? Because this blows up your node_modules directory up considerably in size. Let's take this example:

$ npm install connect-mongo jade mongoose express stylus
$ du -sh node_modules
 17M    node_modules/

Wait, what? 17 megabytes? Considering that those are not large modules, and nodejs modules are basically plain text, 17M is a lot. Even more interestingly, all those modules have a .npmignore file. So where does this bloat come from? As I said, it's misconfigured .npmignore files. Let's look at the file from stylus:

$ cat node_modules/stylus/.npmignore

Looks reasonable. It ignores test, docs, examples. All the stuff you don't need when you're just using the package. But wait, those ignored directories do exist:

$ test -d node_modules/stylus/test && echo "test directory exists"
test directory exists

Well, that's interesting. Or not. Because it's a known bug. Unresolved since two months now. And many more similar bugs related to npmignore mechanism are also still open.

So pretty plase, check whether your .npmignore works. Probably the easiest way is to create the pack manually and see what npm put into it. Here on the example of zion, which includes only the bare minimum and nothing more:

$ npm pack
tar ./zion-0.0.5.tgz

$ tar -tf zion-0.0.5.tgz