npm-packlist
v11.1.0
Published
Get a list of the files to add from a folder into an npm package
Downloads
57,613,440
Keywords
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:
If a
package.jsonfile is found, and it has afileslist, then ignore everything that isn't matched byfiles. Always include the root readme, license, licence and copying files, if they exist, as well as the package.json file itself.If there's no
package.jsonfile (or it has nofileslist), and there is a.npmignorefile, then ignore all the files in the.npmignorefile.If there's no
package.jsonwith afileslist, and there's no.npmignorefile, but there is a.gitignorefile, then ignore all the files in the.gitignorefile.Everything in the root
node_modulesis 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.Unless they're explicitly included (by being in a
fileslist, or a!negatedrule in a relevant.npmignoreor.gitignore), always ignore certain common cruft files:- .npmignore and .gitignore files (their effect is in the package already, there's no need to include them in the package)
- editor junk like
.*.swp,._*and.*.origfiles .npmrcfiles (these may contain private configs)- The
node_modules/.binfolder - Waf and gyp cruft like
/build/config.gypiand.lock-wscript - Darwin's
.DS_Storefiles because wtf are those even npm-debug.logfiles at the root of a project
You can explicitly re-include most of these with a negated ignore file rule. A small set of files are always ignored regardless of
filesrules:.git,.npmrc, andnode_modules.
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.
Interpretation of the files list
Each entry in the package.json files list is interpreted as a glob
pattern, expanded against the package root using node's built-in
fs.globSync.
Literal paths (lib, src/index.js) are valid globs and resolve to
themselves; pattern entries (dist-*, **/*.js) resolve to every
matching entry on disk; entries that match nothing are silently dropped.
When an entry matches a directory, the directory and all of its contents
are included. This is a documented convenience: writing
"files": ["dist"] does not require also writing "dist/**".
A leading ! makes the entry a negation, removing matches from the
result. Negations are evaluated in order, so a later positive entry
can re-include something a previous negation excluded.
Pattern semantics are anchored to the package root
Glob patterns are evaluated as paths relative to the package root, not as basename-anywhere matchers. In particular:
"*.js"matches.jsfiles at the package root, not at every depth. Use"**/*.js"to match at every depth."!readme.md"removes only the rootreadme.md. Use"!**/readme.md"to removereadme.mdat every depth.
Negations only exclude paths that exist on disk at packlist time: a !path
entry that matches nothing is silently dropped, same as a positive entry
that matches nothing. A typo'd negation has no effect.
Interaction with .npmignore
If a package.json files array is present, any top-level
.npmignore and .gitignore files are ignored: the files array is
the sole source of inclusion at the package root.
.npmignore files in subdirectories are still honored within their
subtree. For example, with this package.json:
{
"files": [ "dir" ]
}a .npmignore at dir/.npmignore (and any nested subdirectory) will
be applied. This means a file listed both in files and excluded by a
nested .npmignore will be excluded from the tarball; if you need such
a file to ship, remove the .npmignore rule.
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.
