# gpm pack

Create a tarball (.tgz) from packages for publishing, matching npm pack behavior.

## Usage

```bash
gpm pack [package-spec...] [flags]
```

## Arguments

* `[package-spec...]` - Package specifications to pack (default: current directory)

## Flags

| Flag                       | Description                                 | Default           |
| -------------------------- | ------------------------------------------- | ----------------- |
| `--dry-run`                | Simulate pack without creating tarball      | `false`           |
| `--ignore-scripts`         | Skip running package scripts during packing | `false`           |
| `--pack-destination <dir>` | Specify output directory                    | Current directory |
| `--scope <scope>`          | Scope for scoped packages (e.g., @myscope)  | None              |
| `--json`                   | Output results in JSON format               | `false`           |

## Description

The `gpm pack` command creates a tarball (.tgz) from packages, following npm pack behavior. This command is useful for creating packages for publishing or distribution.

### Package Specs

* **Current directory**: `gpm pack` (default)
* **Package folders**: `gpm pack ./my-package ./another-package`
* **Existing tarballs**: `gpm pack package.tgz another.tgz`

### Supported Operations

* **Directory Packing**: Pack package directories
* **Tarball Repacking**: Repack existing tarballs
* **Multiple Packages**: Pack multiple packages at once
* **Dry Run**: Preview what would be packed

## Examples

### Basic Usage

```bash
# Pack current directory
gpm pack

# Pack specific folder
gpm pack ./my-package

# Pack multiple packages
gpm pack ./package1 ./package2
```

### Advanced Usage

```bash
# Dry run to see what would be packed
gpm pack --dry-run

# Pack to specific directory
gpm pack --pack-destination /tmp/packages

# Pack with JSON output
gpm pack --json

# Pack existing tarball
gpm pack package.tgz
```

### Package Scripts

```bash
# Pack with package scripts
gpm pack

# Pack without running scripts
gpm pack --ignore-scripts
```

## Package Structure

### Required Files

A valid package must contain:

* **package.json**: Package metadata and configuration
* **README.md**: Package documentation (recommended)
* **LICENSE**: Package license (recommended)

### Package.json Requirements

```json
{
  "name": "com.company.package",
  "version": "1.0.0",
  "description": "Package description",
  "author": "Author Name",
  "license": "MIT",
  "unity": "2021.3",
  "keywords": ["unity", "package"]
}
```

### File Inclusion

GPM includes files based on:

* **package.json**: Files specified in `files` field
* **.npmignore**: Files to exclude (if present)
* **.gitignore**: Git ignored files (if no .npmignore)
* **Default patterns**: Common package files

## Output

### Human Format (Default)

```
Package Packed Successfully
─────────────────────────────
Package:  com.company.package
Version:  1.0.0
Tarball:  com.company.package-1.0.0.tgz
Size:     2.5 MB
Files:    15
─────────────────────────────
Package com.company.package-1.0.0.tgz created successfully
```

### JSON Format (`--json`)

```json
{
  "success": true,
  "package": "com.company.package",
  "version": "1.0.0",
  "tarball": "com.company.package-1.0.0.tgz",
  "size": "2.5 MB",
  "files": 15,
  "message": "Package com.company.package-1.0.0.tgz created successfully"
}
```

## Dry Run Mode

### Preview Packing

```bash
# See what would be packed
gpm pack --dry-run
```

### Dry Run Output

```
Pack Preview
─────────────────────────────
Package:  com.company.package
Version:  1.0.0
Files to include:
  package.json
  README.md
  LICENSE
  Runtime/
  Editor/
  Tests/

Files to exclude:
  .git/
  node_modules/
  .DS_Store

─────────────────────────────
Run without --dry-run to create tarball
```

## Multiple Package Packing

### Pack Multiple Packages

```bash
# Pack multiple packages
gpm pack ./package1 ./package2 ./package3

# Pack with different destinations
gpm pack ./package1 --pack-destination /tmp/pkg1
gpm pack ./package2 --pack-destination /tmp/pkg2
```

### Batch Packing

```bash
# Pack all packages in directory
gpm pack ./packages/*

# Pack with JSON output
gpm pack ./packages/* --json
```

## Package Scripts

### Script Execution

GPM runs package scripts during packing:

* **prepack**: Before packing starts
* **pack**: During packing process
* **postpack**: After packing completes

### Script Control

```bash
# Run all scripts (default)
gpm pack

# Skip all scripts
gpm pack --ignore-scripts

# Run specific scripts
gpm pack  # Scripts run automatically
```

## Error Handling

### Common Errors

* **No package.json**: `No package.json found in directory`
* **Invalid package.json**: `Invalid package.json: missing required fields`
* **Package validation failed**: `Package validation failed: invalid package name`
* **File access error**: `Failed to read file: permission denied`

### Troubleshooting

1. **No package.json**: Create package.json with `gpm init`
2. **Invalid package.json**: Check required fields and format
3. **Package validation**: Verify package name follows reverse-DNS format
4. **File access**: Check file permissions and disk space

## Best Practices

### Package Preparation

```bash
# Initialize package first
gpm init

# Validate package structure
gpm pack --dry-run

# Pack for publishing
gpm pack
```

### File Organization

```bash
# Use .npmignore for file exclusion
echo "*.tmp" >> .npmignore
echo "*.log" >> .npmignore

# Organize package structure
mkdir -p Runtime Editor Tests
```

### Version Management

```bash
# Update version before packing
gpm version patch

# Pack with new version
gpm pack

# Tag version
git tag v1.0.0
```

## Integration with Publishing

### Pack and Publish Workflow

```bash
# 1. Pack package
gpm pack

# 2. Test tarball
gpm pack package.tgz --dry-run

# 3. Publish package
gpm publish package.tgz --access=scoped
```

### CI/CD Integration

```bash
# Pack in CI
gpm pack --json > pack-results.json

# Publish in CI
gpm publish --access=scoped
```

## Related Commands

* [`gpm publish`](/cli-reference/cli/publishing/publish.md) - Publish package to registry
* [`gpm init`](/cli-reference/cli/project/init.md) - Initialize new package
* [`gpm version`](/cli-reference/cli/utilities/version.md) - Manage package versions

## See Also

* [Publishing Overview](/cli-reference/cli/publishing.md)
* [Package Guidelines](https://github.com/gpm-sh/docs/blob/main/packages/README.md)
* [Unity Package Manager](https://github.com/gpm-sh/docs/blob/main/engines/unity.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gpm.sh/cli-reference/cli/publishing/pack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
