Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
npm-packlist
Advanced tools
The npm-packlist package is used to generate a list of files that would be included in an npm package publish. This includes filtering out files that are not meant to be published, such as those specified in `.npmignore` or the `.gitignore` files, and including those specified in the `files` array in `package.json`. It's useful for package authors to understand and control what gets included in their published npm package.
Generate packlist
This feature allows you to generate a list of files that would be included if you were to publish the current package. The code sample demonstrates how to use npm-packlist to get an array of file paths that are included in the package's publish list.
const packlist = require('npm-packlist');
packlist().then(files => {
console.log(files)
});
Customize packlist with package.json
While not a direct feature of npm-packlist, the package respects the `files` field in `package.json`. This allows users to explicitly specify which files or directories should be included in the publish. The code sample shows how to specify a list of files and directories in `package.json` that should be included in the npm package.
{
"files": [
"lib/**/*",
"bin/*",
"README.md"
]
}
The 'glob' package provides functionality to match files using the patterns the shell uses, like stars and stuff. It's similar to npm-packlist in that it can be used to select files to include in a package, but it does not directly integrate with npm's packaging logic or respect `.npmignore` and `files` in `package.json`.
The 'ignore' package is used to filter file paths according to the rules found in `.gitignore` and `.npmignore` files. It's similar to npm-packlist in its purpose of filtering out files, but it's more focused on the ignore rules and doesn't directly generate a list of files for npm packaging.
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 packlist = require('npm-packlist')
const tar = require('tar')
const packageDir = '/path/to/package'
const packageTarball = '/path/to/package.tgz'
packlist({ path: packageDir })
.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.json
file is found, and it has a files
list,
then ignore everything that isn't in files
. Always include the
readme, license, notice, changes, changelog, and history files, if
they exist, and the package.json file itself.
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.
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.
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.
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:
.*.swp
, ._*
and .*.orig
files.npmrc
files (these may contain private configs)node_modules/.bin
folder/build/config.gypi
and .lock-wscript
.DS_Store
files because wtf are those evennpm-debug.log
files at the root of a projectYou 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.
package.json
and .npmignore
rulesFor simplicity, it is best to use either a files
list in package.json
or a .npmignore
file, and not both. If you only use one of these
methods, you can skip this documentation section.
The files
list in package.json
is used to direct the exploration of the
tree. In other words, that's all the walker will ever look at when
exploring that level.
In some cases this can lead to a .npmignore
file being ignored. If a
directory is listed in files
, then any rules in a root or nested
.npmignore
files 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.
Conversely, with this package.json:
{
"files": ["dir/subdir"]
}
a .npmignore
file at dir/.npmignore
will not be honored.
Any specific file matched by a glob or filename in the package.json files
list will be included, and cannot be excluded by any .npmignore
files in
nested directories, or by a .npmignore
file in the root package
directory, unless that root .npmignore
file is also in the files
list.
The previous (v1) implementation used in npm 6 and below treated
package.json
as a special sort of "reverse ignore" file. That is, it was
parsed and handled as if it was a .npmignore
file with !
prepended to
all of the globs in the files
list. In order to include children of a
directory listed in files
, they would also have /**
appended to them.
This is tricky to explain, but is a significant improvement over the previous (v1) implementation used in npm 6 and below, with the following beneficial properties:
{"files":["lib"]}
in package.json
, then the walker will
still ignore files such as lib/.DS_Store
and lib/.foo.swp
. The
previous implementation would include these files, as they'd be matched
by the computed !lib/**
ignore rule.{"files":["lib/a.js","lib/b.js"]}
in package.json
, and a
lib/.npmignore
containing a.js
, then the walker will still include
the two files indicated in package.json
, and ignore the
lib/.npmignore
file. The previous implementation would mark these
files for inclusion, but then exclude them when it came to the nested
.npmignore
file. (Ignore file semantics dictate that a "closer" ignore
file always takes precedence.)lib/pkg-template/package.json
will be included, and its
files
list will not have any bearing on other files being included or
skipped. When treating package.json
as just Yet Another ignore file,
this was not the case, leading to difficulty for modules that aim to
initialize a project.In general, this walk should work as a reasonable developer would expect. Matching human expectation is tricky business, and if you find cases where it violates those expectations, please let us know.
Same API as ignore-walk, just hard-coded file list and rule sets.
The Walker
and WalkerSync
classes take a bundled
argument, which
is a list of package names to include from node_modules. When calling
the top-level packlist()
and packlist.sync()
functions, this
module calls into npm-bundled
directly.
4.0.0 (2022-03-03)
FAQs
Get a list of the files to add from a folder into an npm package
The npm package npm-packlist receives a total of 6,650,489 weekly downloads. As such, npm-packlist popularity was classified as popular.
We found that npm-packlist demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.