Skip to main content
The @knitkit/runtime package is the browser-side core of knitkit. It fetches remote manifests, negotiates shared dependency versions, injects a native ES module import map, and exposes lazy module loading — all in under 5 KB with zero dependencies. Import from @knitkit/runtime in your host application’s entry point; every export below is available from that single specifier.

registerRemotes(remotes, options?)

registerRemotes is the primary entry point for a host application. It fetches (or accepts inline) each remote’s manifest, runs shared-dependency version negotiation, injects the resulting import map into the document, and stores registrations so that subsequent loadRemote calls can resolve module URLs. Call it once, before any loadRemote call or <script type="module"> that imports federated modules.

Parameters

RegisterRemotesInput[]
required
An array of remote descriptors. Each entry identifies a remote by name and tells the runtime where to find its manifest.
RegisterRemotesOptions
Optional configuration for the host side of negotiation.

Returns

Promise<NegotiationResult> — resolves once all manifests are fetched and the import map has been injected.
Record<string, { version: string; url: string; integrity?: string; source: string }>
For each shared package, the single winning version that was selected and the URL that will be used for it. source is the remote name (or "host") that contributed the winning copy.
Record<string, Record<string, string>>
Fallback import map scopes for any non-singleton packages where a participant’s required version range could not be satisfied by the winner. Each key is a URL scope prefix; its value maps package names to the scoped fallback URL.
ImportMap
The fully computed import map payload — { imports, scopes?, integrity? } — that was injected into the document. Useful for server-side rendering where you need to serialize the map into HTML.
ResolutionReport
A human-readable resolution report keyed by package name. Each entry contains the winner details, any fallbacks, and a boolean conflict flag. Inspect this with getShareInfo() during debugging.

Example


loadRemote(specifier)

loadRemote dynamically imports an exposed module from a previously registered remote. It resolves the module URL from the stored registration, delegates to the native import() function, and returns the module’s default export when one is present, or the full namespace object otherwise.

Parameters

string
required
A string of the form "<remoteName>/<exposeKey>". The ./ prefix on the expose key is optional — "checkout/CartWidget" and "checkout/./CartWidget" resolve identically. The remoteName must match a name passed to registerRemotes.

Returns

Promise<T> — the module’s default export if the loaded module has one, otherwise the full ES module namespace object.

Type safety

When you run knitkit types sync, the command generates a declaration file that augments the RemoteModules interface in @knitkit/runtime. After that, specifiers listed in RemoteModules are fully typed; unregistered specifiers fall back to unknown.

Example

Error codes


getLastResult()

getLastResult returns the NegotiationResult produced by the most recent registerRemotes call, or null if registerRemotes has not been called yet. Use this for post-boot inspection or logging without needing to hold a reference to the registerRemotes promise.

getShareInfo()

getShareInfo returns the ResolutionReport from the last negotiation. The report is a flat, human-readable summary of which version won for each package, which participants fell back to a scoped copy, and whether any conflicts were detected. Unlike getLastResult, this function always returns a valid object (an empty report when negotiation has not run).

Example


negotiateShared(manifests, hostShared?)

negotiateShared is the low-level version negotiation engine. registerRemotes calls it internally — most application code never needs to call it directly. It is re-exported from @knitkit/node for server-side rendering scenarios where you want to run negotiation in Node.js without touching the DOM.

Parameters

Array<{ name: string; manifest: Manifest; baseUrl: string }>
required
Parsed manifests to negotiate. baseUrl is the absolute URL of the manifest document; relative shared[].url values are resolved against it.
Record<string, HostSharedDecl>
Optional host-side shared declarations, identical in shape to the options.hostShared accepted by registerRemotes.
string
Optional base URL for resolving relative URLs within hostShared entries. Defaults to "".

Returns

NegotiationResult — synchronously; no network I/O is performed.

injectImportMap(importMap)

injectImportMap merges a computed import map into the document’s <script type="importmap"> element. If no such element exists it creates one and appends it to <head>. If one already exists it merges imports, scopes, and integrity objects into the existing map.
ImportMap
required
An object with imports (required), scopes (optional), and integrity (optional) keys — the standard import map shape.
Call injectImportMap before any <script type="module"> that resolves specifiers through the map. Native import maps are immutable once a browser’s module graph has started loading, and injecting a map after that point has no effect on already-started loads.
injectImportMap requires a DOM (document must be defined). In Node.js or edge runtimes, it throws KNIT_ERR_IMPORT_MAP_INJECTION_FAILED. Use @knitkit/node for server-side rendering, which handles import map serialization without touching the DOM.

validateManifest(input, sourceLabel)

validateManifest parses and validates an unknown value against the knitkit manifest spec. It checks the spec version, name format, exposes and shared structure, semver validity, and field types. On success it returns a fully-typed Manifest object; on failure it throws a FedkitError.
unknown
required
The raw parsed JSON (e.g. the result of JSON.parse). Pass it before using any manifest fields so you get structured error messages rather than runtime property access failures.
string
required
A human-readable label included in error messages — typically the manifest URL or file path. Used for diagnostics only.
Throws FedkitError with code KNIT_ERR_MANIFEST_INVALID when any field is missing or invalid. The error’s .message identifies the specific field and the .suggestion field provides a corrective action.

Example


FedkitError and isFedkitError

All errors thrown by @knitkit/runtime are instances of FedkitError. Use isFedkitError to distinguish them from unexpected runtime errors in a catch block.

FedkitError fields

FedkitErrorCode
A stable string code identifying the error category. See the full list below.
string
A human-readable description including the specific package name, URL, or field that caused the error.
string | undefined
An actionable suggestion for resolving the error, when one is available.

Error codes

Example