# `Denox.Npm`
[🔗](https://github.com/gsmlg-dev/denox/blob/v0.9.0/lib/denox/npm.ex#L1)

Pre-bundling support for npm/jsr packages via `deno bundle`.

For packages that don't vendor cleanly, bundle them into a self-contained
JS file that can be loaded into any runtime.

## Workflow

    # 1. Bundle a package at build-time
    Denox.Npm.bundle!("npm:zod@3.22", "priv/bundles/zod.js")

    # 2. Load into a runtime
    {:ok, rt} = Denox.runtime()
    :ok = Denox.Npm.load(rt, "priv/bundles/zod.js")

    # 3. Use the package
    {:ok, result} = Denox.eval(rt, "globalThis.zod.string().parse('hello')")

The `deno` CLI is only required at bundle-time, not at runtime.

# `bundle`

```elixir
@spec bundle(String.t(), String.t(), keyword()) :: :ok | {:error, String.t()}
```

Bundle an npm/jsr package into a single JavaScript file.

Uses `deno bundle` to produce a self-contained JS file with all
dependencies inlined. The output is an ES module.

## Options

  - `:config` - path to deno.json for import map resolution
  - `:platform` - target platform: "deno" (default) or "browser"
  - `:minify` - minify the output (default: false)

Returns `:ok` or `{:error, message}`.

## Examples

    Denox.Npm.bundle("npm:zod@3.22", "priv/bundles/zod.js")
    Denox.Npm.bundle("npm:lodash-es@4.17", "priv/bundles/lodash.js", minify: true)

# `bundle!`

```elixir
@spec bundle!(String.t(), String.t(), keyword()) :: :ok
```

Bundle an npm/jsr package, raising on failure.

Same as `bundle/3` but raises on error.

# `bundle_file`

```elixir
@spec bundle_file(String.t(), String.t(), keyword()) :: :ok | {:error, String.t()}
```

Bundle from an entrypoint file rather than a specifier.

Useful when you have a local .ts/.js entrypoint that imports
multiple packages.

## Options

  Same as `bundle/3`.

Returns `:ok` or `{:error, message}`.

## Examples

    Denox.Npm.bundle_file("lib/js/entrypoint.ts", "priv/bundles/app.js")
    Denox.Npm.bundle_file("lib/js/entrypoint.ts", "priv/bundles/app.js", minify: true)

# `load`

```elixir
@spec load(Denox.runtime(), String.t()) :: :ok | {:error, String.t()}
```

Load a bundled JavaScript file into a runtime.

Evaluates the bundle file contents in the runtime, making the
exported values available via `globalThis`.

Returns `:ok` or `{:error, message}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
