LinuxCommandLibrary

npm-pkg

Manage npm package artifacts

TLDR

Get the value of a specific property

$ npm pkg get [name]
copy

Get multiple properties at once
$ npm pkg get [name|version|...]
copy

Get multiple values across all workspaces
$ npm pkg get [name] [version] [[--ws|--workspaces]]
copy

Get a nested or array property value
$ npm pkg get [contributors[0].email]
copy

Set a property to a specific value
$ npm pkg set [property]=[value]
copy

Set multiple properties at once
$ npm pkg set [property1]=[value1] [property2]=[value2]
copy

Delete a property from package.json
$ npm pkg delete [scripts.build]
copy

Auto-fix common errors in package.json
$ npm pkg fix
copy

SYNOPSIS

npm pack
npm 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.

SEE ALSO

npm install(1), npm publish(1), npm link(1), tar(1)

Copied to clipboard