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_INVALID_EXPORT
NEXUS_INVALID_EXPORT means a module lists a token in exports that it neither provides nor sees through an import, or a module it does not import. Nexus.create reports it inside NEXUS_BLUEPRINT_INVALID.
Reproduce NEXUS_INVALID_EXPORT
The region exports PowerRouter from Engineering, which provides only ReactorCore. 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 IReactorCore {
readonly output: number;
}
interface IPowerRouter {
divert(): number;
}
const REACTOR = new Token<IReactorCore>('ReactorCore');
const POWER_ROUTER = new Token<IPowerRouter>('PowerRouter');
class FusionReactor implements IReactorCore {
readonly output = 1.21;
}
const Engineering = defineModule({
name: 'Engineering',
providers: [provide(REACTOR, { useClass: FusionReactor })],
exports: [REACTOR, POWER_ROUTER],
});
const thin = await Nexus.create(Engineering).catch((error: unknown) => error);
if (!isNexusError(thin, 'NEXUS_BLUEPRINT_INVALID')) throw thin;
const line = thin.errors[0]?.message; // -> '[NEXUS_INVALID_EXPORT] token=PowerRouter module=Engineering. https://nexus.js.org/errors/NEXUS_INVALID_EXPORT'
console.log(line);
const full = await Nexus.create(Engineering, { plugins: [errors()] }).catch(
(error: unknown) => error,
);
if (!isNexusError(full, 'NEXUS_BLUEPRINT_INVALID')) throw full;
const text = full.errors[0]?.message.split('\n'); // -> ['[NEXUS_INVALID_EXPORT] Engineering exports PowerRouter, which it neither provides nor sees through an import.', ' Fix: provide PowerRouter in Engineering, or import the module that exports it.']
console.log(text);Try it: save the region as error.ts and run npx tsx error.ts. It prints the one-line message of the inner error, then the full text as an array of two strings. The first line says that Engineering exports PowerRouter, which it neither provides nor sees through an import, and the second line is the Fix: line. Then save the fix region below as fixed.ts and run npx tsx fixed.ts. It prints 0.605.
Fix NEXUS_INVALID_EXPORT
NEXUS_INVALID_EXPORT is fixed by exporting only what the module provides or imports. Either provide the token in the module or import the module that exports it; if the module should not export it at all, remove it from exports. Here Engineering gains a PlasmaRouter bound to POWER_ROUTER.
import { Nexus, Token, defineModule, provide } from '@nexusdi/core';
interface IReactorCore {
readonly output: number;
}
interface IPowerRouter {
divert(): number;
}
const REACTOR = new Token<IReactorCore>('ReactorCore');
const POWER_ROUTER = new Token<IPowerRouter>('PowerRouter');
class FusionReactor implements IReactorCore {
readonly output = 1.21;
}
class PlasmaRouter implements IPowerRouter {
static deps = [REACTOR] as const;
constructor(private readonly reactor: IReactorCore) {}
divert() {
return this.reactor.output / 2;
}
}
const Engineering = defineModule({
name: 'Engineering',
providers: [
provide(REACTOR, { useClass: FusionReactor }),
provide(POWER_ROUTER, { useClass: PlasmaRouter }),
],
exports: [REACTOR, POWER_ROUTER],
});
await using ship = await Nexus.create(Engineering);
const diverted = ship.get(POWER_ROUTER).divert(); // -> 0.605
console.log(diverted);Fields of InvalidExportError
InvalidExportError carries these fields:
token: the display name of the exported token or module.module: the module whoseexportslists it.