Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

Share lunas as a package

Publish luna types as an npm package with definePackage, and use npm packages in your lunas.

Luna types can be shared between apps as an npm package. The app installs the package, exports its luna types and runs them as its own.

Make a package

Wrap the package’s luna types in definePackage, with the package’s name. It returns the same luna types.

import { defineLuna, definePackage, t, table } from "luna";

export const { Session } = definePackage("@acme/luna-sessions", {
  Session: defineLuna({
    name: "Session",
    key: { id: t.string() },
    schema: { values: table({ k: t.string(), v: t.string() }, { primaryKey: ["k"] }) },
    handlers: (h) => ({
      put: h({ args: { k: t.string(), v: t.string() } }, (ctx, { k, v }) => {
        ctx.db.values.upsert({ k, v });
      }),
    }),
  }),
});

Publish the package as JavaScript, with type declarations beside it as for any TypeScript package. Luna leaves .ts files in packages out of a deploy.

Add a luna field to the package’s package.json, so Luna knows the package holds lunas. Import from "luna" and do not list it as a dependency, because the app provides it.

{
  "name": "@acme/luna-sessions",
  "type": "module",
  "exports": "./index.js",
  "luna": {}
}

Use a package in an app

Install the package in the folder that holds luna.config.ts, then export its luna types from your lunas module.

cd luna
npm install @acme/luna-sessions
export { List } from "./list.ts";
export { Session } from "@acme/luna-sessions";

The package’s luna types run in your app, with its own names and URLs. Usage and billing show what they cost on a line of their own, under the package’s name.

Use npm packages in your lunas

Your lunas can import any npm package in the dependencies of the package.json beside luna.config.ts. luna deploy sends each one with your code from the node_modules folder there, as npm installed it.

  • devDependencies are left out, and so are .ts, .map and .md files inside packages.
  • Packages with native .node addons cannot run on Luna, and the deploy says which one is the problem. An optional dependency with one is left out instead.
  • With pnpm, set node-linker=hoisted in .npmrc.
  • An app and its packages can be up to 256 MiB. A deploy that is too big lists the largest packages.

A redeploy sends only the files that changed.

Next steps

Was this page helpful?