Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

npm-packlist

npm28.1mISC10.0.0TypeScript support: definitely-typed

Get a list of the files to add from a folder into an npm package

readme

npm-packlist

Get a list of the files to add from a folder into an npm package.

These can be handed to tar like so to make an npm package tarball:

const Arborist = require('@npmcli/arborist')
const packlist = require('npm-packlist')
const tar = require('tar')
const packageDir = '/path/to/package'
const packageTarball = '/path/to/package.tgz'

const arborist = new Arborist({ path: packageDir })
arborist.loadActual().then((tree) => {
  packlist(tree)
    .then(files => tar.create({
      prefix: 'package/',
      cwd: packageDir,
      file: packageTarball,
      gzip: true
    }, files))
    .then(_ => {
      // tarball has been created, continue with your day
    })
  })

This uses the following rules:

  1. If a package.json file is found, and it has a files list, then ignore everything that isn't in files. Always include the root readme, license, licence and copying files, if they exist, as well as the package.json file itself. Non-root readme, license, licence and copying files are included by default, but can be excluded using the files list e.g. "!readme".
  2. If there's no package.json file (or it has no files list), and there is a .npmignore file, then ignore all the files in the .npmignore file.
  3. If there's no package.json with a files list, and there's no .npmignore file, but there is a .gitignore file, then ignore all the files in the .gitignore file.
  4. Everything in the root node_modules is ignored, unless it's a bundled dependency. If it IS a bundled dependency, and it's a symbolic link, then the target of the link is included, not the symlink itself.
  5. Unless they're explicitly included (by being in a files list, or a !negated rule in a relevant .npmignore or .gitignore), always ignore certain common cruft files:

    1. .npmignore and .gitignore files (their effect is in the package already, there's no need to include them in the package)
    2. editor junk like .*.swp, ._* and .*.orig files
    3. .npmrc files (these may contain private configs)
    4. The node_modules/.bin folder
    5. Waf and gyp cruft like /build/config.gypi and .lock-wscript
    6. Darwin's .DS_Store files because wtf are those even
    7. npm-debug.log files at the root of a project

      You can explicitly re-include any of these with a files list in package.json or a negated ignore file rule.

Only the package.json file in the very root of the project is ever inspected for a files list. Below the top level of the root package, package.json is treated as just another file, and no package-specific semantics are applied.

Interaction between package.json and .npmignore rules

In previous versions of this library, the files list in package.json was used as an initial filter to drive further tree walking. That is no longer the case as of version 6.0.0.

If you have a package.json file with a files array within, any top level .npmignore and .gitignore files will be ignored.

If a directory is listed in files, then any rules in nested .npmignore files within that directory will be honored.

For example, with this package.json:

{
  "files": [ "dir" ]
}

a .npmignore file at dir/.npmignore (and any subsequent sub-directories) will be honored. However, a .npmignore at the root level will be skipped.

Additionally, with this package.json:

{
  "files": ["dir/subdir"]
}

a .npmignore file at dir/.npmignore will be honored, as well as dir/subdir/.npmignore.

Any specific file matched by an exact filename in the package.json files list will be included, and cannot be excluded, by any .npmignore files.

API

Same API as ignore-walk, except providing a tree is required and there are hard-coded file list and rule sets.

The Walker class requires an arborist tree, and if any bundled dependencies are found will include them as well as their own dependencies in the resulting file set.

changelog

Changelog

10.0.0 (2024-11-25)

⚠️ BREAKING CHANGES

  • this module is now compatible with the following node versions: ^20.17.0 || >=22.9.0
  • bun.lockb is now included in the strict ignorelist

    Features

  • 9f74fd3 #168 add bun.lockb to ignorelist (#168) (@antongolub)

    Bug Fixes

  • 67fef03 update engines to ^20.17.0 || >=22.9.0 (#254) (@wraithgar)

    Chores

  • fd425fa bump @npmcli/eslint-config from 4.0.5 to 5.0.1 (#250) (@dependabot[bot])
  • 3b66b5a #252 bump @npmcli/arborist from 7.5.4 to 8.0.0 (#252) (@dependabot[bot])
  • e688d26 bump @npmcli/template-oss from 4.23.3 to 4.23.4 (#251) (@dependabot[bot], @npm-cli-bot)

9.0.0 (2024-09-25)

⚠️ BREAKING CHANGES

  • npm-packlist now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • 4679b12 #247 align to npm 10 node engine range (@hashtagchris)

    Dependencies

  • b0500f4 #247 `ignore-walk@7.0.0`

    Chores

  • 46ed801 #245 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (#245) (@dependabot[bot])
  • 625ddca #240 bump @npmcli/arborist from 6.5.1 to 7.5.4 (#240) (@dependabot[bot])
  • e2eb28d #247 run template-oss-apply (@hashtagchris)
  • 3c27bb3 #230 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • fa9e80d #242 bump @npmcli/template-oss from 4.22.0 to 4.23.3 (#242) (@dependabot[bot])
  • cb4a823 #230 postinstall for dependabot template-oss PR (@lukekarrys)

8.0.2 (2024-01-03)

Bug Fixes

  • 728d68a #214 handling of directory glob (#214) (@mohd-akram)
  • 837af58 #210 avoid modifying singleton variable (#210) (@mohd-akram)

8.0.1 (2023-12-06)

Bug Fixes

  • 8fc4df1 #204 always ignore .npmrc files at every level (@wraithgar)
  • cd5ddbd #205 preserve slashes in specified files (#205) (@mohd-akram)

Dependencies

Chores

  • 20fe0cd #207 bump @npmcli/template-oss from 4.21.1 to 4.21.2 (#207) (@dependabot[bot], @lukekarrys)
  • f8be7bd #202 bump @npmcli/template-oss from 4.19.0 to 4.21.1 (#202) (@dependabot[bot], @lukekarrys)
  • 8e1d900 #204 tests reflect fixed ignore-walk rules (@wraithgar)
  • 6d7cbe9 #181 postinstall for dependabot template-oss PR (@lukekarrys)
  • 80ec501 #181 bump @npmcli/template-oss from 4.18.1 to 4.19.0 (@dependabot[bot])
  • f327738 #179 postinstall for dependabot template-oss PR (@lukekarrys)
  • a770a96 #179 bump @npmcli/template-oss from 4.18.0 to 4.18.1 (@dependabot[bot])

8.0.0 (2023-08-24)

⚠️ BREAKING CHANGES

  • The files array can now be used to exclude non-root readme, license, licence, and copying files.

Bug Fixes

  • 24344a2 #173 exclude non-root README.md/LICENSE files (#173) (@AaronHamilton965, @rahulio96)

7.0.4 (2022-12-07)

Bug Fixes

  • e5256de #149 skip missing optional deps when bundling, closes npm/cli#5924 (#149) (@nlf)

7.0.3 (2022-12-07)

Bug Fixes

  • c6f2b69 #147 treat glob the same as globstar (#147) (@lukekarrys)

7.0.2 (2022-10-26)

Bug Fixes

  • d5b653c #140 account for directories and files prefixed with ./ (#140) (@wraithgar)

7.0.1 (2022-10-17)

Dependencies

7.0.0 (2022-10-04)

Features

7.0.0-pre.1 (2022-10-03)

⚠️ BREAKING CHANGES

  • if npm-shrinkwrap.json is included in your .npmignore, the shrinkwrap will now be excluded from your packlist.

Features

  • 5e80968 #131 npm-shrinkwrap.json files can now be ignored (#131) (@fritzy)

7.0.0-pre.0 (2022-09-26)

⚠️ BREAKING CHANGES

  • tree is now the first parameter
  • the arborist tree must now be provided in the options and will not be generated for you. the npm-packlist bin has also been removed.

Features

  • 87c778e make the required tree the first parameter (@lukekarrys)
  • 123875a remove dependency on arborist, require tree to be passed in (@nlf)

6.0.1 (2022-09-23)

Dependencies

6.0.0 (2022-09-21)

⚠️ BREAKING CHANGES

  • this module now follows a strict order of operations when applying ignore rules. if a files array is present in the package.json, then rules in .gitignore and .npmignore files from the root will be ignored.
  • npm-packlist is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • c37371b #88 change interactions between files array and ignore files to be more consistent (#88) (@nlf)
  • a2c96ef #123 postinstall for dependabot template-oss PR (@lukekarrys)

5.1.3 (2022-08-25)

Dependencies

  • bump npm-bundled from 1.1.2 to 2.0.0 (#113) (de5e96b)

5.1.2 (2022-08-23)

Dependencies

  • bump npm-normalize-package-bin from 1.0.1 to 2.0.0 (#110) (c97d1a8)

5.1.1 (2022-05-31)

Bug Fixes

  • correctly ignore .gitignore when a .npmignore is present (#108) (da1ba4a)

5.1.0 (2022-05-25)

Features

5.0.4 (2022-05-19)

Bug Fixes

  • do not pack workspaces by default (0f31f71)
  • respect gitignore and npmignore files in workspace roots (839e6e8)

5.0.3 (2022-05-04)

Bug Fixes

  • strip leading ./ from files array entries (#97) (9f519b7)

5.0.2 (2022-04-21)

Bug Fixes

  • normalize win32 paths before globbing (16f1343)

5.0.1 (2022-04-20)

Dependencies

5.0.0 (2022-04-06)

⚠ BREAKING CHANGES

  • this module no longer supports synchronous usage

Features

Bug Fixes

  • remove polynomial regex (b2e2f7b)
  • replace deprecated String.prototype.substr() (#85) (79d6f7e)

Dependencies

  • bump ignore-walk from 4.0.1 to 5.0.1 (#87) (0e241f5)

4.0.0 (2022-03-03)

⚠ BREAKING CHANGES

  • This drops support for node10 and non-LTS versions of node12 and node14

Bug Fixes

Dependencies

  • update glob requirement from ^7.1.6 to ^7.2.0 (#70) (d6b34ac)
  • update npm-bundled requirement from ^1.1.1 to ^1.1.2 (#71) (2a5e140)