npm-pkg
Manage npm package artifacts
TLDR
Get the value of a specific property
Get multiple properties at once
Get multiple values across all workspaces
Get a nested or array property value
Set a property to a specific value
Set multiple properties at once
Delete a property from package.json
Auto-fix common errors in package.json
SYNOPSIS
npm packnpm pack <package-spec>...npm pack [--dry-run] [--json] [--pack-destination <dir>]
PARAMETERS
--dry-run
Shows what files would be included in the tarball without actually creating it.--json
Outputs data in JSON format, useful for programmatic consumption.--pack-destination <dir>
Specifies the directory where the tarball should be saved. Defaults to the current working directory.--foreground-scripts
Runs prepack, pack, and postpack scripts in the foreground.--ignore-scripts
Prevents the execution of prepack, pack, and postpack lifecycle scripts.--workspace <name>, --workspaces
Used within monorepos to specify a particular workspace or operate on all workspaces.--include-workspace-root
When used with --workspaces, includes the root package of the monorepo in the operation.
DESCRIPTION
npm pack creates a compressed tarball (.tgz file) of an npm package, allowing for easy distribution, installation, or local testing without publishing to a registry. It takes into account the files field in package.json and respects .npmignore (or .gitignore if .npmignore is absent) to determine which files to include. This command is invaluable for developers who need to share a package with others for testing, or to prepare a package for publication to a private or public registry.
The resulting tarball contains all the necessary code and package.json metadata, making it self-contained and ready for installation via npm install <tarball-file>. It also executes prepack and postpack scripts if defined in package.json.
CAVEATS
It is assumed that "npm-pkg" refers to the npm pack command, as npm-pkg is not a standard Linux command or a direct subcommand of npm.
The npm pack command does not publish the package; it only prepares the tarball. The behavior regarding included and excluded files is crucial and depends heavily on package.json's files array and the presence of .npmignore or .gitignore files.
EXCLUDING FILES
npm pack automatically excludes certain files and directories like .git, CVS, .svn, .hg, node_modules, .npmignore, README.md (and other readme formats), CHANGES.md, and LICENSE.md unless explicitly included. If a .npmignore file is present in the package root, its rules override .gitignore. If no .npmignore is present, .gitignore is used.
PACKAGE LIFECYCLE SCRIPTS
npm pack triggers prepack and postpack lifecycle scripts defined in package.json. prepack runs before the tarball is generated, often used for compilation or preprocessing. postpack runs after the tarball is created.
HISTORY
The npm CLI, and with it the npm pack command, has been an integral part of the Node.js ecosystem since its inception. npm was first released in 2010 by Isaac Schlueter. The ability to pack a module into a tarball has always been fundamental for local testing, offline distribution, and preparing packages for publication, preceding the more recent additions of features like workspaces. Its design reflects the UNIX philosophy of creating small, composable tools, with pack serving as a critical step in the software supply chain for Node.js modules.


