# Error injecting singletons in Aurelia 2

**URL:** <https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444>\
**Category:** vNext\
**Created:** [June 12, 2024, 9:18am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444 "2024-06-12T09:18:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 12, 2024, 9:18am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/1 "2024-06-12T09:18:27Z")

</div>

For testing purposes, I create a DI container like:

```typescript
const container = DI.createContainer()

// This works
const numberValueConverter = container.get(NumberValueConverter)

```

But when I want to inject another value converter, which injects `NumberValueConverter` as well:

```typescript
@inject(NumberValueConverter, IEventAggregator)
export class ByteFormatValueConverter extends AbstractLocaleValueConverter {
    constructor(
        private readonly _numberValueConverter:NumberValueConverter,
        eventAggregator:IEventAggregator
    ) {
        super(eventAggregator);
    }

```

It fails at this line

```typescript
const bytesFormatValueConverter = container.get(ByteFormatValueConverter);

```

with an cryptic error message:

```auto
    AUR0003:NumberValueConverter

      16 | //container.makeGlobal();
      17 |
    > 18 | const numberValueConverter = container.get(NumberValueConverter);
         | ^
      19 | numberValueConverter.getLocale()
      20 | // const bytesFormatValueConverter = numberValueConverter
      21 | const bytesFormatValueConverter = container.get(ByteFormatValueConverter);

      at createMappedError (node_modules/@aurelia/kernel/src/errors.ts:8:50)
      at Resolver.resolve (node_modules/@aurelia/kernel/src/di.ts:566:17)
      at Container.get (node_modules/@aurelia/kernel/src/di.container.ts:375:27)
      at Container.map (node_modules/@aurelia/kernel/src/di.container.ts:657:15)
          at Array.map (<anonymous>)
      at Factory.construct (node_modules/@aurelia/kernel/src/di.container.ts:626:55)
      at Resolver.resolve (node_modules/@aurelia/kernel/src/di.ts:569:96)
      at Container.get (node_modules/@aurelia/kernel/src/di.container.ts:371:29)
      at Object.<anonymous> (tests/value-converters/byte-format-value-converter.test.ts:18:40)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

```

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 12, 2024, 11:33am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/2 "2024-06-12T11:33:21Z")

</div>

You are getting `AUR0003`, which means Aurelia error code 0003, which means you have a cyclic dependency somewhere in `NumberValueConverter`. Maybe have a look at the class again?

For the crypticness error code, it can be solved by telling the test running to use the development build of Aurelia, similar to this section doc [Errors | The Aurelia 2 Docs](https://docs.aurelia.io/developer-guides/error-messages#enabling-development-debug-information-enhancement)

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 12, 2024, 6:31pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/3 "2024-06-12T18:31:15Z")

</div>

Well, I have no circular references in `NumberValueConverter`.

`ByteFormatValueConverter` → `AbstractLocaleValueConverter` injects `NumberValueConverter` → `AbstractLocaleValueConverter`

This code works with `@autoinject()` of Aurelia 1.  
See here: [aurelia-components/tests/value-converters/byte-format-value-converter.test.ts at master · telekom-mms/aurelia-components · GitHub](https://github.com/telekom-mms/aurelia-components/blob/master/tests/value-converters/byte-format-value-converter.test.ts)

[ByteFormatValueConverter](https://github.com/telekom-mms/aurelia-components/blob/master/src/value-converters/byte-format-value-converter.ts)

[NumberValueConverter](https://github.com/telekom-mms/aurelia-components/blob/master/src/value-converters/number-value-converter.ts)

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 12, 2024, 6:44pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/4 "2024-06-12T18:44:27Z")

</div>

It works when I write:

```typescript
@inject(IEventAggregator)
export class ByteFormatValueConverter extends AbstractLocaleValueConverter {
    private readonly _numberValueConverter = resolve(NumberValueConverter)
    constructor(eventAggregator:IEventAggregator) {
        super(eventAggregator);
    }

```

instead of (which is the most intuitive way, comming from Aurealia 1)

```typescript
@inject(NumberValueConverter, IEventAggregator)
export class ByteFormatValueConverter extends AbstractLocaleValueConverter {
    constructor(
        private readonly _numberValueConverter:NumberValueConverter,
        eventAggregator:IEventAggregator
    ) {
        super(eventAggregator);
    }

```

That not very intuitive, what is the reason for that?

The following way does not work:

```typescript
export class ByteFormatValueConverter extends AbstractLocaleValueConverter {
    constructor(
        private readonly _numberValueConverter = resolve(NumberValueConverter),
        eventAggregator:IEventAggregator = resolve(IEventAggregator)
    ) {
        super(eventAggregator);
    }

```

because both variables are type of `EventAggregator` then.

 ![Bildschirmfoto 2024-06-12 um 20.43.43](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/a/abe7b2ef32778b500be121e258069ad74faf1b3e.jpeg)

I don’t unterstand it at all.

---

<div class="post-metadata">

**Author:** ![m-gallesio](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/m-gallesio/32/1288_2.png) [@m-gallesio](https://discourse.aurelia.io/u/m-gallesio)\
**Post date:** [June 13, 2024, 7:16am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/5 "2024-06-13T07:16:05Z")

</div>

> [@mreiche](#):
>
> instead of (which is the most intuitive way, comming from Aurealia 1)

This is no longer supported due to changes in decorators, see [here](https://ilikekillnerds.com/2024/04/psa-aurelia-2-beta-15-introduces-breaking-changes-to-decorators/) (and [here](https://discourse.aurelia.io/t/property-injection-in-in-au1-au2-resolve/5418)).

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 7:51am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/6 "2024-06-13T07:51:22Z")

</div>

That means, the documentation is outdated and wrong? [Dependency injection (DI) | The Aurelia 2 Docs](https://docs.aurelia.io/getting-to-know-aurelia/overview#using-decorators)

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 2:54pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/7 "2024-06-13T14:54:15Z")

</div>

`@inject` works, and is supported.  
Can you try to create a repro based on this [Aurelia app - conventions webpack - StackBlitz](https://stackblitz.com/edit/au2-conventions?file=src%2Fmy-app.html)

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 3:15pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/8 "2024-06-13T15:15:39Z")

</div>

I’m trying here: [Aurelia @inject test - StackBlitz](https://stackblitz.com/edit/au2-conventions-lumanr) but I get different errors now.

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 3:44pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/9 "2024-06-13T15:44:40Z")

</div>

Would you mind to take a look @bigopon ? I’m stuck, because this code works locally and I can only guess, if it’s typescript, ‘tsconfig’ or any other module.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 4:09pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/10 "2024-06-13T16:09:41Z")

</div>

1. @mreiche I don’t see you import & register the value converter anywhere, so do this in your `main.ts`:

```ts
Aurelia.register(NumberValueConverter).app(...).start();

```

1. And there seems to be missing abstract class type support for `@inject` decorator, so change to

```ts
@inject(IEventAggregator)
export class AbstractLocaleValueConverter {
  ...

```

for now.

1. Also make sure you change target to es2022 in tsconfig.json

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 4:24pm UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/11 "2024-06-13T16:24:52Z")

</div>

Both is present and I changed the target to “es2022”. Still erroring. Do you see my changes?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 14, 2024, 12:12am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/12 "2024-06-14T00:12:20Z")

</div>

Yes that’s the 2nd point you need to change. I’ve updated my post earlier to make it clearer

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 14, 2024, 4:21am UTC](https://discourse.aurelia.io/t/error-injecting-singletons-in-aurelia-2/5444/13 "2024-06-14T04:21:16Z")

</div>

Now I unterstood. I can’t use `@inject` together with `abstract class`. Should we file a bug somewhere?
