Skip to Content
ErrorsNEXUS_INVALID_MODULE

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_MODULE

NEXUS_INVALID_MODULE means a value used as a module is neither a definition from defineModule(), which includes a forRoot() instance, nor a class declared with @Module. Nexus.create reports it inside NEXUS_BLUEPRINT_INVALID for an entry in imports, load() and the module option of get() throw it directly, and so does defineModule() for a config that is no object with a name. Nexus.create throws it directly for a root object with keys a root does not take. The module option also raises it for a real module that is not part of the graph.

Reproduce NEXUS_INVALID_MODULE

The region lists the provider class FusionReactor under imports of 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, defineModule, isNexusError } from '@nexusdi/core'; import { errors } from '@nexusdi/errors'; interface IReactorCore { readonly output: number; } class FusionReactor implements IReactorCore { readonly output = 1.21; } // FusionReactor is a provider class, listed under imports by mistake. const Meridian = defineModule({ name: 'Meridian', imports: [FusionReactor] }); 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_INVALID_MODULE] received=the function FusionReactor path=Meridian. https://nexus.js.org/errors/NEXUS_INVALID_MODULE' 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_INVALID_MODULE] the function FusionReactor is not a module (imported by Meridian).', ' Fix: create one with defineModule(), or decorate a class with @Module.'] console.log(text);

Try it: save the region as error.ts and run npx tsx error.ts. It prints the one-line message, whose path names the module whose imports held the value, then the full text as an array of two strings: the function FusionReactor is not a module, and the Fix: line. Then save the fix region below as fixed.ts and run npx tsx fixed.ts. It prints 1.21.

Fix NEXUS_INVALID_MODULE

NEXUS_INVALID_MODULE is fixed by importing modules only. A provider class goes in a module’s providers, and the root imports that module. Here Engineering provides and exports REACTOR.

import { Nexus, Token, defineModule, provide } from '@nexusdi/core'; interface IReactorCore { readonly output: number; } const REACTOR = new Token<IReactorCore>('ReactorCore'); class FusionReactor implements IReactorCore { readonly output = 1.21; } const Engineering = defineModule({ name: 'Engineering', providers: [provide(REACTOR, { useClass: FusionReactor })], exports: [REACTOR], }); const Meridian = defineModule({ name: 'Meridian', imports: [Engineering] }); await using ship = await Nexus.create(Meridian); const output = ship.get(REACTOR).output; // -> 1.21 console.log(output);

Fields of InvalidModuleError

InvalidModuleError carries these fields:

  • received: a description of the value, such as the function FusionReactor.
  • path: the import path that reached the value, empty outside the walk.
  • otherCopy: true when another copy of @nexusdi/core made the value. In module federation, list @nexusdi/core in shared with singleton: true.