This page documents the 0.4 release candidates of @nexusdi/core. The newest on npm is 0.4.0-rc.1. The documentation for 0.3.2, the current release, is at nexus.js.org. Try the release candidate.
NEXUS_MODULE_OPTIONS_MISSING
NEXUS_MODULE_OPTIONS_MISSING means a configurable module, one defined with options, was imported as it is, without forRoot() or forRootAsync(), so its options token has no value. Nexus.create reports it inside NEXUS_BLUEPRINT_INVALID.
Reproduce NEXUS_MODULE_OPTIONS_MISSING
The region defines Comms with the options token COMMS_OPTIONS and imports Comms bare into Meridian. Without a plugin, core prints a one-line message that starts with the code and ends with the address of this page. With errors() from @nexusdi/errors registered (npm install @nexusdi/errors@next), the same error carries the full text and its fix.
import { Nexus, Token, defineModule, isNexusError } from '@nexusdi/core';
import { provide } from '@nexusdi/core';
import { errors } from '@nexusdi/errors';
interface CommsOptions {
readonly frequency: number;
}
interface ISubspaceLink {
readonly frequency: number;
}
const COMMS_OPTIONS = new Token<CommsOptions>('CommsOptions');
const SUBSPACE_LINK = new Token<ISubspaceLink>('SubspaceLink');
class SubspaceRelay implements ISubspaceLink {
static deps = [COMMS_OPTIONS] as const;
readonly frequency: number;
constructor(options: CommsOptions) {
this.frequency = options.frequency;
}
}
const Comms = defineModule({
name: 'Comms',
options: COMMS_OPTIONS,
providers: [provide(SUBSPACE_LINK, { useClass: SubspaceRelay })],
exports: [SUBSPACE_LINK],
});
const Meridian = defineModule({ name: 'Meridian', imports: [Comms] });
const thin = await Nexus.create(Meridian).catch((error: unknown) => error);
if (!isNexusError(thin, 'NEXUS_BLUEPRINT_INVALID')) throw thin;
const line = thin.errors[0]?.message; // -> '[NEXUS_MODULE_OPTIONS_MISSING] module=Comms. https://nexus.js.org/errors/NEXUS_MODULE_OPTIONS_MISSING'
console.log(line);
const full = await Nexus.create(Meridian, { plugins: [errors()] }).catch(
(error: unknown) => error,
);
if (!isNexusError(full, 'NEXUS_BLUEPRINT_INVALID')) throw full;
const text = full.errors[0]?.message.split('\n'); // -> ['[NEXUS_MODULE_OPTIONS_MISSING] Comms is configurable and was imported without forRoot() or forRootAsync().', ' Fix: import Comms.forRoot(options) or Comms.forRootAsync({ useFactory }).']
console.log(text);Try it: save the region as error.ts and run npx tsx error.ts. It prints the one-line message, then the full text. The Fix: line of the full text names forRoot() and forRootAsync(). Then save the fix region below as fixed.ts and run npx tsx fixed.ts. It prints 1420.
Fix NEXUS_MODULE_OPTIONS_MISSING
NEXUS_MODULE_OPTIONS_MISSING is fixed by importing the module through forRoot(options), or forRootAsync({ useFactory, deps }) for options computed at startup. Call either once and import the result: each call makes a new module instance. Configurable Modules covers both.
import { Nexus, Token, defineModule, provide } from '@nexusdi/core';
interface CommsOptions {
readonly frequency: number;
}
interface ISubspaceLink {
readonly frequency: number;
}
const COMMS_OPTIONS = new Token<CommsOptions>('CommsOptions');
const SUBSPACE_LINK = new Token<ISubspaceLink>('SubspaceLink');
class SubspaceRelay implements ISubspaceLink {
static deps = [COMMS_OPTIONS] as const;
readonly frequency: number;
constructor(options: CommsOptions) {
this.frequency = options.frequency;
}
}
const Comms = defineModule({
name: 'Comms',
options: COMMS_OPTIONS,
providers: [provide(SUBSPACE_LINK, { useClass: SubspaceRelay })],
exports: [SUBSPACE_LINK],
});
const Meridian = defineModule({
name: 'Meridian',
imports: [Comms.forRoot({ frequency: 1420 })],
});
await using ship = await Nexus.create(Meridian);
const frequency = ship.get(SUBSPACE_LINK).frequency; // -> 1420
console.log(frequency);Try it: in fixed.ts, replace Comms.forRoot({ frequency: 1420 }) with Comms and run it again. Nexus.create rejects with a BlueprintError that holds one NEXUS_MODULE_OPTIONS_MISSING error for Comms. Put .forRoot({ frequency: 1420 }) back and the program prints 1420 again.
Fields of ModuleOptionsError
ModuleOptionsError carries these fields for this code:
module: the configurable module imported without options.issues: empty for this code. NEXUS_INVALID_MODULE_OPTIONS uses the same class and fills it.