Guides
Debug

Debug

Import scripts of redi more than once

💡

[redi]: You are loading scripts of redi more than once! This may cause undesired behavior in your application. Maybe your dependencies added redi as its dependency and bundled redi to its dist files. Or you import different versions of redi.

This error happens when you import scripts of redi more than once. This usually happens when

  1. Some dependency in your workspace uses redi as well. However, it don't declare redi as an external dependency.
  2. Some dependency in your project uses a different version of redi from yours.
  3. You are working on a monorepo and repos uses different versions of redi.

redi use Symbol and instanceof internally. So things may not work when you import redi more than once. To fix this problem:

  1. Contact the maintainer of these dependencies and ask them set redi as an external dependency.
  2. The dependency may re-exports API of redi. In such case you should use these re-exported API instead of using redi directly.
  3. Use the same version of redi across your monorepo.

Could not find dependency registered on

💡

[redi]: It seems that you register "undefined" as dependency on the X parameter of "XXX". Please make sure that there is not cyclic dependency among your TypeScript files, or consider using "forwardRef".

This errors happens when redi is trying to figure out what dependencies XXX relies on but a dependency is resolved to undefined.

Let's assume that the dependency resolved to undefined is YYY.

  1. YYY must be a class type dependency.
  2. Due to how TypeScript is transpiled into JavaScript. YYY is declared first and later assigned to the constructor function of YYY.
  3. There is cycle dependency among your TypeScript files that leads to YYY being evaluated as undefined when Inject decorator is trying to register YYY as a dependency on XXX.

To fix this problem, you can use forwardRef

import { Inject, forwardRef } from '@wendellhu/redi'
 
class XXX {
    constructor(@Inject(forwardRef(() => YYY)) private readonly _yyy: YYY) {}
}
 

... or fix cycle dependency in your codebase. We recommend using dpdm (opens in a new tab) to find cycle dependency in your codebase. You could traverse the dependency tree staring from the file that contains XXX, and break cycles those contains the file.