Search code examples
npmnpmignore

How to debug .npmignore?


How can package files (these to be published) be listed to debug records in .npmignore?

I'm looking for something like equivalent of git ls-files for .gitignore.

The only way I have found so far is to pack the package and then list the archive which I find a bit clumsy:

npm pack
tar -tzf <package-id>.tgz

Solution

  • As Mike 'Pomax' Kamermans mentioned in comment the fact that .npmignore and .gitignore use the same syntax can be leveraged:

    git ls-files -co --exclude-per-directory=.npmignore
    

    The command above lists exactly files that are not npm-ignored according to .npmignore file. (On top of that npm automatically ignores some other entries like node_modules.)

    Git ls-files command generally lists combinations of files in working directory and index.

    • -c option says show cached files
    • -o show 'other', i.e. untracked files
    • --exclude-per-directory=.npmignore use .npmignore as name of files of ignore entries

    EDIT: Since the approach above has bunch of exceptions - files that will never or always included regardless of content of the .npmignore - I find it unreliable. Following command is heavyweight but reliable:

    file_name=$(npm pack) && tar -ztf $file_name && rm $file_name
    

    It packages the project, lists package files and at the end removes created package.