INJECUTE_NOTHING_TO_REPLACE
A registration replaces a key that nothing is registered under.
How to fix it
replace: true replaces a registration of this container or a parent. Nothing has this key, which usually means a registration is missing: a module that was not applied, or a typo in the key. Register the original first, or remove replace: true if this is the first registration.
Example
ts
const app = new DIContainer().addSingleton('db', createDb);
app.fork({ isolated: true }).addInstance('dbb', fakeDb, { replace: true }); // throws: a typo
app.fork({ isolated: true }).addInstance('db', fakeDb, { replace: true }); // replaces "db"Why it throws
replace: true says that something is registered under the key. When nothing is, the replacement would quietly become a new service that nothing uses, and the original you meant to replace would still be in place. Common causes:
- a typo in the key;
- a module that should register the original wasn't applied, or is applied after the replacement: apply modules first, then replace;
- a test override for a service the app no longer has.
If this is the first registration of the key, remove replace: true.
Handling it in code
ts
import { InjecuteError } from 'injecute';
try {
// ...
} catch (error) {
if (error instanceof InjecuteError && error.code === 'INJECUTE_NOTHING_TO_REPLACE') {
// error.message explains what happened; error.path is the resolution chain
}
}