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.
- Default:
false
- Type:
Boolean
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.
- Default:
false
- Type:
Boolean
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.
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.
- Default:
undefined
- Type:
String
(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.
- Default:
null
- Type:
null
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.
- Default:
false
- Type:
Boolean
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:
Project structurepackage.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.
Output
Similar Reads
npm ping Command
npm ping command is a simple but powerful utility provided by npm to check if your npm registry is accessible, this command allows developers to verify the connection between their projects and the NPM registry, it sends a request to the registry and returns a response indicating whether the registr
3 min read
npm bin Command
The npm bin command is a lesser-known but incredibly useful command in the Node.js ecosystem. It provides information about the location where npm installs globally executable binaries or locally installed binaries for the current project. In this article, weâll explore the details of the npm bin co
4 min read
npm react-native Command
React Native is a popular framework for building cross-platform mobile applications using JavaScript and React. One of the key tools you'll use when working with React Native is npm (Node Package Manager), which is a package manager for JavaScript and plays an integral role in managing dependencies
4 min read
npm edit command
The command that can simplify the editing process of the project's files is npm edit. This command is useful for opening a file editor directly from the command line which allows users to make changes to the projectâs files effortlessly. This article will provide a comprehensive overview of the npm
3 min read
NPM Docs Command
The npm docs command is a convenient way to access the official documentation of any npm package directly from the command line. It opens the documentation page of the specified package in your default web browser, making it easier for developers to quickly find and explore package details, APIs, an
4 min read
npm run all Command
The npm run-all is a powerful command used in Node.js projects to execute multiple npm scripts sequentially or in parallel. This approach is especially useful for automating tasks such as linting, testing, building, and deploying the application. By organizing these tasks into the separate scripts i
3 min read
npm exec command
The npm exec command is a powerful feature introduced in npm v7 allowing the users to execute binaries or scripts defined in the node_modules/.bin or those available globally without needing to include them in the PATH. It simplifies running scripts that are part of the project or installed packages
4 min read
NPM Diff Command
The npm diff command is used to compare changes between different versions of a package, changes in your working directory, or even changes in dependencies. This command helps developers identify modifications made to package files, dependencies, or configurations, allowing for a clear view of what
4 min read
npm cache command
Using npm (Node package manager) to manage packages efficiently is very important for the smooth functioning of applications. The 'npm cache' command plays an important role in this process by helping developers manage the local package cache. It helps us inspect, clean, and manage the cache to ensu
4 min read
npm dedupe command
The npm dedupe command is a helpful tool for optimizing your Node.js projects by reducing the size of the node_modules directory. It works by removing duplicate packages across dependencies by keeping the most suitable versions of each package to minimize duplication. Here is the comprehensive guide
9 min read