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

Use JavaScript modules and libraries

Declare an npm package in the App's package.json, import it from server.js or ui.js, and let Charming bundle it at build time.

An App is a package, so a library is a dependency. Name it in package.json under dependencies with a semver range, import it from server.js or ui.js, and Charming resolves and bundles it when it builds the App. The published App makes no request to a package host or CDN: the browser runs one bundled inline module, and the server runs its own bundle. See Build an App as a package for the full contract.

An App saved before packages, with module/ui/styles, inlined a reviewed library build into ui instead; see Migrate to packages.

Declare and import

dependencies is one map shared by the server and the browser. The build installs it once and bundles whatever each entry imports, so a package only ui.js imports never reaches the server bundle, and the other way round.

{
  "name": "notes",
  "version": "1.0.0",
  "type": "module",
  "dependencies": { "dompurify": "^3.4.17", "marked": "^18.1.2" },
  "charming": { "name": "Notes" }
}
// ui.js
import DOMPurify from 'dompurify';
import { marked } from 'marked';
import { api, onStateChange } from 'charming:ui/[email protected]';

async function render() {
  const note = await api.read();
  document.getElementById('app').innerHTML = DOMPurify.sanitize(marked.parse(note.markdown));
}
render();
onStateChange(render);

marked passes raw HTML in the markdown through, so the example sanitizes its output before assigning innerHTML: a note another person can edit could otherwise run script with each viewer’s window.charming authority. Keep platform modules such as charming:storage/[email protected] and charming:ui/[email protected] out of dependencies; they come from the import alone. A relative import names a .js, .mjs, or .json file in the tree, so a helper you wrote lives beside the entries, not in a package.

Charming checks every import before it accepts the save. A bare import that names no package in dependencies, or a relative import that names no file in the tree, is refused with invalid_module or invalid_ui and the file, line, and column. Declaring a package you never import is allowed. Ask your agent to review the package and its license before it adds the dependency, the same as any other code it ships.

Pick a package that bundles

Choose a package that:

  • ships ESM or CommonJS that runs in a browser or a WinterTC server runtime;
  • fits the limits: source files other than styles.css total 1024 KiB, and compiled output is 2 MiB per target;
  • does not fetch more JavaScript, CSS, workers, WebAssembly, models, or other chunks at runtime; and
  • has source and a license you can review.

Packages that need Node built-ins, native addons, install scripts, CSS imports, emitted assets, WebAssembly, workers, or runtime code loading fail the build: unsupported_node_builtin names the offending fs or path import, unsupported_native_addon names the addon. An App may pull in at most 64 package instances, counting transitive ones, before the build fails with package_limit. Prefer a small local helper when a library would add much more code than the App needs.

The build resolves public npm packages with install scripts disabled, retains exact versions and archive integrity, and compiles from those retained archives without network access. Edits that keep the same dependencies may reuse the retained lock; change the range when you want a newer release.

When npm cannot install a package you declared, the build fails with app_build_failed, specifier set to the package name, and a message that names the package, its range, and the reason, such as npm could not install [email protected]: no matching version or package not found. Fix the name or range in package.json and save again. When the failing package is a transitive one, the message gives npm’s error code instead, such as ETARGET. When npm cannot reach the registry, Charming retries the build itself.

Keep the execution environments separate

A package imported from ui.js runs in the browser with the rest of your UI and receives the same window.charming authority as your own browser code, so treat it as part of the App rather than as a harmless asset. A package imported from server.js runs in the server sandbox, where only WinterTC globals exist and outbound requests come from charming:network/[email protected].

Declaring charming.permissions.server.fetch does not let the browser reach a library host. It permits backend fetch calls from server routes and does not widen browser script-src or connect-src. Declaring an image origin changes only img-src, and declaring a charming.permissions.browser["connect-src"] origin changes only connect-src. See External files and CSP.

Before you ship

  • Review the package source and license when possible.
  • Check the build result: failed names the file and the unsupported import.
  • Confirm that the App makes no follow-up network requests for code.
  • Test the App at its standalone URL and in a chat embed.

Was this page helpful?