Creating a module
Create an AntelopeJS module from a template, configure its entry point and build, and load it in your project.
Initialize a module
Create a module when you need a unit with its own lifecycle, dependencies, and configuration. If you do not have a project to load it yet, complete Quick Start first.
npx @antelopejs/core module init ./my-module --template blank-typescript --pm pnpm --no-git-init
The command copies the template and installs its dependencies. Without these flags, it asks for each answer. Add interface/ and implementations/ when the module exposes a contract. A module that only consumes other interfaces does not need those directories. Other templates can start from a different layout.
Module structure
For a module that publishes its own contract, this layout keeps the interface declarations apart from their implementations. A consumer-only module can omit both directories.
my-module/
├── package.json # Module metadata with antelopeJs config
├── tsconfig.json # TypeScript configuration
├── src/
│ ├── index.ts # Entry point with lifecycle functions
│ ├── interface/ # Interface contract
│ │ └── index.ts
│ └── implementations/ # Interface implementation
│ └── index.ts
└── dist/ # Compiled output
The interface/ directory holds the contract: the types, the proxy declarations, and the wrapper functions that describe what the module offers. The implementations/ directory holds the logic that fulfills the contract.
Package configuration
The antelopeJs field in package.json tells the core how to load the module.
{
"name": "@your-org/my-module",
"version": "1.0.0",
"main": "dist/index.js",
"antelopeJs": {
"implements": ["@your-org/my-module"],
"baseUrl": "dist",
"moduleAliases": {},
"defaultConfig": {}
}
}
| Field | Description |
|---|---|
implements | Array of npm package names from which consumers import the interface |
baseUrl | Base directory, relative to the module folder, for the paths mappings |
paths | Import path mappings, resolved from baseUrl |
moduleAliases | Import aliases, each mapped to a path relative to the module folder |
defaultConfig | Configuration that ajs project modules add writes into the project's entry for the module |
test | Path to the test configuration file |
The implements array lists the npm package names that expose the interface contract. When the interface is embedded, the module lists its own package name, because consumers import the interface from it, for example @your-org/my-module/interface. When the interface lives in a standalone package, the module lists that package name instead, for example @antelopejs/interface-api.
An interface starts embedded inside its module and can move to a standalone package later. The Interfaces concept page and the Exporting Interfaces guide describe this move.
Entry point
The module entry point exports the lifecycle functions that the core calls in a fixed order. They control initialization, startup, and teardown.
import { ImplementInterface } from "@antelopejs/interface-core";
export async function construct(config: unknown): Promise<void> {
ImplementInterface(
await import("./interface"),
await import("./implementations"),
);
}
export function start(): void {}
export async function drain(): Promise<void> {}
export async function stop(): Promise<void> {}
export async function destroy(): Promise<void> {}
The lifecycle has five phases:
| Phase | When it runs | Purpose |
|---|---|---|
construct | First, for each module | Initialize resources and register interface implementations |
start | After all modules are constructed | Begin operations. Every interface is resolved by now. |
drain | At shutdown, for a started module, before any stop | Finish the work in flight while every module still runs |
stop | At shutdown, for a module that started | Stop ongoing operations |
destroy | After stop, and after a failed construct() or start() | Release resources and close connections |
The core calls drain on every started module at the same time, and calls the first stop once every drain has returned or 5 seconds have passed. It never calls drain when it stops, reloads, or removes one module at run time. An HTTP server stops accepting connections in drain and waits for the requests it is still answering:
export async function drain(): Promise<void> {
await new Promise<void>((resolve) => {
server.close(() => resolve());
});
}
Modules shows the whole server and what happens after the 5 seconds.
A provider module can also export a provide callback. It runs before any module constructs and publishes config variables. See Modules.
The construct function receives the module's configuration object. Use it to open database connections, initialize state, or adjust behavior to the settings the user provides.
If a required module rejects during construct() or start(), launch fails and the runtime tries to clean up. Other modules may still be initializing at that point. Make destroy() safe after partial initialization. That includes a failed start(), after which the module never receives stop(). See Error Handling.
TypeScript configuration
A typical tsconfig.json for an AntelopeJS module enables decorators and type declarations.
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": ["src/**/*.ts"]
}
Because include names only src, the module build leaves out antelope.config.ts, the test configuration, and the test files. Setting antelopeJs.baseUrl does not change TypeScript's output or the package's main entry point.
Importing interfaces
To use an interface that another module provides, add the interface package as a dependency and import from it.
import { GetUser } from "@your-org/interface-users";
const user = await GetUser("user-123");
The import resolves to the interface declaration, not to the implementation. At runtime, the core connects the call to the module that implements the interface. Your code depends on the contract and never on one implementation.
The application configuration selects the provider, not the consumer. A replacement provider must support the contract version and the behavior your consumer needs. Its configuration and data migrations may still differ.
Adding the module to a project
To load the module at runtime, register it in your project's antelope.config.ts.
import { defineConfig } from "@antelopejs/interface-core/config";
export default defineConfig({
name: "my-project",
modules: {
"my-module": {
source: {
type: "local",
path: "./my-module",
watchDir: "src",
installCommand: "pnpm exec tsc",
reloadCommand: "pnpm exec tsc",
},
},
},
});
The source field accepts several types. A local source points to a directory on disk, which suits development. A module can also come from an npm package or a git repository.
The core loads the module's compiled main entry point, so the module must be built first. The configuration above runs installCommand when the module loads and reloadCommand when it reloads. --watch alone does not compile a hand-written local source that has no build command.
From the project root, run ajs project dev --watch. Check that the module constructs and starts. Edit its source and check that the new version loads. If the runtime cannot find dist/index.js, check the compiler output, the package's main, and the source's build command before you look at interface bindings.
See also
- Modules covers module states and the lifecycle.
- Testing shows how to test your module.
- Architecture explains how the core runs modules.