# Installation (https://lazy-layers-cache.vercel.app/docs/installation)

> Install lazy-layers-cache and check Node.js, module, and TypeScript requirements.



Install with `npm install lazy-layers-cache`. Version 9.0.0 supports Node.js 22 or later, ships ESM, CommonJS and TypeScript declarations, and has one runtime dependency, `msgpackr`. You can run an L1-only cache without Redis or a broker.

## Install [#install]

<CodeGroup>
  ```bash title="npm"
  npm install lazy-layers-cache
  ```

  ```bash title="pnpm"
  pnpm add lazy-layers-cache
  ```

  ```bash title="yarn"
  yarn add lazy-layers-cache
  ```
</CodeGroup>

Integrations are optional peer dependencies. Install the ones you use; importing the package never loads them, and a missing one raises `MissingPeerDependencyError` with the install command.

| You use                                                      | Also install                                 |
| ------------------------------------------------------------ | -------------------------------------------- |
| Redis L2 or Redis Pub/Sub, including `setupCache({ redis })` | `ioredis`                                    |
| `RabbitMQEventBus`                                           | `amqplib`                                    |
| `NatsEventBus`                                               | `@nats-io/transport-node @nats-io/jetstream` |
| LZ4 or Snappy compression (recommended with Redis)           | `lz4-napi snappy`                            |

Use Node.js 22+ with NATS: its current transitive dependency declares that minimum.

Without `lz4-napi`, values that would use LZ4 fall back to Snappy, then Zstandard (Node 22.15+), then no compression. `setupCache` waits for the native codecs to load; when you construct caches yourself, `await nativeCodecsReady` once at startup or pass the modules to `registerNativeCodecs`.

## Choose a module format [#choose-a-module-format]

<CodeGroup>
  ```js title="ESM: cache.mjs"
  import { setupCache } from 'lazy-layers-cache'

  const cache = await setupCache({ namespace: 'demo', redis: false })
  try {
    console.log(await cache.getOrSet('greeting', async () => 'Hello'))
  } finally {
    await cache.close()
  }
  ```

  ```js title="CommonJS: cache.cjs"
  const { setupCache } = require('lazy-layers-cache')

  async function main() {
    const cache = await setupCache({ namespace: 'demo', redis: false })
    try {
      console.log(await cache.getOrSet('greeting', async () => 'Hello'))
    } finally {
      await cache.close()
    }
  }

  main().catch((error) => {
    console.error(error)
    process.exitCode = 1
  })
  ```
</CodeGroup>

Run `node cache.mjs` or `node cache.cjs`. Both print `Hello`. For ESM in `.js` files, set `"type": "module"` in your application's `package.json`.

## TypeScript [#typescript]

Import types from the package without installing a separate `@types` package. Set the key and value types once when you create a cache:

```ts title="src/cache/index.ts"
import { setupCache } from 'lazy-layers-cache'

interface User {
  id: string
  name: string
}

export const cache = await setupCache<string, User>({
  namespace: 'users-api',
  redis: false,
})
```

`getOrSet` now returns `Promise<User | undefined>` and checks the loader's return type. For a Node.js ESM project, use `"module": "NodeNext"` and `"moduleResolution": "NodeNext"` with `"type": "module"`. Keep the `.js` extension in relative imports that will run as compiled JavaScript.

## Connect shared infrastructure [#connect-shared-infrastructure]

Managed Redis setup requires Redis 6 or newer, a reachable `REDIS_URL`, and access to the commands used by the cache and event bus. Set `redis: { required: true }` when your deployment depends on shared caching so missing configuration fails at startup.

See [production setup](/docs/setups/production) for Redis ACLs and upgrade guidance. RabbitMQ and NATS are alternative [event buses](/docs/guides/event-buses), not requirements for starting with Redis.

## Where to next [#where-to-next]

* [Quickstart](/docs/quickstart) shows reads, invalidation, and cleanup.
* [API reference](/docs/reference/api#production-setup) describes `setupCache` options and ownership.
