Search code examples
node.jsnpmgitignorepackage-managersnpmignore

How can I whitelist a single file in a directory in .npmignore?


I'm trying to make npm download only a single file in a directory on npm install of the package.

The directory looks like:

+- dist/
   +- 1.0.0/
   +- 1.0.1/
   +- ...lots of other dirs...
   +- file.js

I want npm to ignore everything but file.js so I tried including the following in my .npmignore:

dist/
!dist/file.js

Yet, npm will still download all the directories in dist when I install the package. I thought this was supposed to work like .gitignore but apparently I am missing something here.


Solution

  • Yes, it is working using glob patterns : https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

    But I would have a different approach :

    dist/*
    dist/.*
    !dist/file.js
    

    in order to no ignore the whole folder but it's content (the 2nd line may not be required).