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_LOAD_GLOBAL_MODULE
NEXUS_LOAD_GLOBAL_MODULE means ship.load() received a module defined with global: true, or a module that imports a global module the graph does not hold yet. Every module’s view of the global modules is computed at create, so a global module can join the graph only at startup. load() rejects with it directly. Nexus.check with the load option reports it inside NEXUS_BLUEPRINT_INVALID.
Reproduce NEXUS_LOAD_GLOBAL_MODULE
The region loads Navigation, a global module, after startup. 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 INavCharts {
plot(to: string): string;
}
const NAV_CHARTS = new Token<INavCharts>('NavCharts');
class StarCharts implements INavCharts {
plot(to: string) {
return `course to ${to}`;
}
}
const Navigation = defineModule({
name: 'Navigation',
global: true,
providers: [provide(NAV_CHARTS, { useClass: StarCharts })],
exports: [NAV_CHARTS],
});
const Meridian = defineModule({ name: 'Meridian' });
await using ship = await Nexus.create(Meridian);
const thin = await ship.load(Navigation).catch((error: unknown) => error);
const line = isNexusError(thin) ? thin.message : null; // -> '[NEXUS_LOAD_GLOBAL_MODULE] module=Navigation. https://nexus.js.org/errors/NEXUS_LOAD_GLOBAL_MODULE'
console.log(line);
await using fullShip = await Nexus.create(Meridian, { plugins: [errors()] });
const full = await fullShip.load(Navigation).catch((error: unknown) => error);
const text = isNexusError(full) ? full.message.split('\n') : null; // -> ["[NEXUS_LOAD_GLOBAL_MODULE] Navigation is global and cannot be loaded after startup, because every module's bindings are already computed.", ' Fix: import Navigation from the root module, or make it non-global.']
console.log(text);Try it: save the region as error.ts and run npx tsx error.ts. load() rejects, and the container keeps running. It prints the one-line message, then the full text. Then save the fix region below as fixed.ts and run npx tsx fixed.ts. It prints course to Kepler-442b.
Fix NEXUS_LOAD_GLOBAL_MODULE
NEXUS_LOAD_GLOBAL_MODULE is fixed by importing the global module from the root module at startup, or by loading modules that are not global and import no global module the graph lacks. Here Navigation is a plain module, and load() adds it.
import { Nexus, Token, defineModule, provide } from '@nexusdi/core';
interface INavCharts {
plot(to: string): string;
}
const NAV_CHARTS = new Token<INavCharts>('NavCharts');
class StarCharts implements INavCharts {
plot(to: string) {
return `course to ${to}`;
}
}
const Navigation = defineModule({
name: 'Navigation',
providers: [provide(NAV_CHARTS, { useClass: StarCharts })],
exports: [NAV_CHARTS],
});
await using ship = await Nexus.create(defineModule({ name: 'Meridian' }));
await ship.load(Navigation);
const course = ship.get(NAV_CHARTS).plot('Kepler-442b'); // -> 'course to Kepler-442b'
console.log(course);Try it: add global: true, under name: 'Navigation', in fixed.ts and run it again. ship.load(Navigation) rejects with a LoadError for Navigation, and nothing after it runs. Remove the line and the program prints course to Kepler-442b again.
The same error appears for a plain module that imports a new global module. With global: true on Navigation as above, define const Wrapper = defineModule({ name: 'Wrapper', imports: [Navigation] }); after it and load Wrapper in place of Navigation. load() rejects with a LoadError whose module is Navigation, the global module, and Wrapper is not added.
Fields of LoadError
LoadError has one field:
module: the name of the global module thatload()found missing from the graph: the module passed to it, or one that module imports.