Open In App

npm pack command

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The npm pack command is a utility provided by Node Package Manager (npm) that allows developers to create tarballs (.tgz files) of npm packages without publishing them to the npm registry, which is useful for testing, private sharing, or checking package contents Before the final implementation, in this article we'll dive into what the npm pack command is, how it works, and provide some helpful examples.

These are the following topics that we are going to discuss:

What is the npm pack Command?

npm pack command creates a compressed tarball of npm packages from your project files, tarball contains files that will be uploaded to the npm registry if you run the npm publish command, making it suitable for previewing packages or sharing your package privately.

Steps to execute the npm pack command

Syntax:

npm pack

Example: For running the following command, It will create a tarball for your current npm project.

npm pack

The tarball will contain all the files that will be published normally, allowing you to inspect them.

Key Benefits of Using npm pack

  • Pre-publish Testing: This allows you to check the contents of your package before it goes live.
  • Private Sharing: You can share .tgz tarballs with others on your private network without going through npm registration.
  • Auditing: By inspecting local package contents you can be sure that no unwanted files will be included.
  • Version Control: Helps you manage package versions efficiently, this is especially true when preparing for multiple release versions.

Why Use the npm pack Command?

When working with npm packages, you often want to ensure that all required files are properly combined and no additional files (such as test files or development configuration) are included, npm pack command provides an easy way to:

  • Verify Package Contents: Check which files will be included in the published version.
  • Prepare Packages for Testing: Allows you to test packages locally before releasing them to the public.
  • Share in Private Networks: Distribute tarball files without publishing to npm.
  • Local Installation: Test the tarball in another project using npm install ./package-name-version.tgz.

How the npm pack Command Works

When you run npm pack it reads your package.json file and bundles necessary files based on the following configurations:

  • package.json "files" field: Determines which files are included.
  • .npmignore or .gitignore: Specifies files to be excluded from package.
  • Defaults: If no specific configurations are set then it will include all files except those typically excluded (node_modules.git, etc.).

Command creates a tarball named after your package and version (package-name-version.tgz) which is saved in current directory.

Managing Initializer Versions

In the context of npm pack, the version of your package (as defined in package.json) plays a critical role, when generating a tarball, the version number gets appended to the filename, like this:

my-pack-gfg-1.0.0.tgz

When testing or distributing multiple versions of same package and it is important to manage your version numbers according to semantic versioning rules to avoid confusion.

Configuration Option

1) --dry-run:

This option indicates that you want npm to only simulate changes it would make without actually making any modifications, it can be used with commands like install, update, dedupe, uninstall, pack and publish however it is not applicable for network-related commands like dist-tags or owner.

  • Defaultfalse
  • TypeBoolean

Syntax:

npm pack --dry-run

2) --json:

When set to true this option ensures that npm outputs data in JSON format instead of standard output, for npm pkg set, it enables parsing set values with JSON.parse() before saving them to package.json, not all npm commands support this option.

  • Defaultfalse
  • TypeBoolean

Syntax:

npm pack --json

3) --pack-destination <dir>:

Defines directory where npm will save tarballs when using npm pack command, default is the current directory.

  • Default"."
  • TypeString

Syntax:

npm pack --pack-destination <directory>  

4) --workspace:

Enables running a command in the context of the configured workspaces of the current project while filtering by the workspaces specified. You can use workspace names, paths to workspace directories, or a parent workspace directory to target specific workspaces.

  • Defaultundefined
  • TypeString (can be set multiple times)

Syntax:

npm install --workspace <workspace_name_or_path>

5) --workspaces:

If set to true, npm will run commands in the context of all configured workspaces, explicitly setting this to false causes npm to ignore workspaces, this affects commands that work with node_modules (like install and update), as well as commands like test, exec, and publish.

  • Defaultnull
  • Typenull or Boolean

Syntax:

npm pack --workspaces 

6) --include-workspace-root:

This option, when enabled, includes the workspace root in the operation when workspaces are enabled for a command, by default, only specified workspaces will be affected, excluding the root project.

  • Defaultfalse
  • TypeBoolean

Syntax:

npm pack --include-workspace-root

Example: For any installable entity (such as a package folder, tarball, git URL or package name), npm will fetch the package to the cache, copy the tarball to the current working directory as <name>-<version>.tgz, and print the filenames to stdout, if the same package is specified multiple times, the file will be overwritten each time.

Project structure:

Untitled
Project structure

package.json:

{
"name": "my-pack-gfg",
"version": "1.0.0",
"main": "index.js",
"files": [
"index.js"
]
}

Now run:

npm pack

Output: It creates a file my-pack-gfg-1.0.0.tgz in your project folder.

Untitled
Output

Next Article

Similar Reads