eidellev / inertiajs-adonisjs

279 stars 17 forks source link

Failure to lookup `@ioc:EidelLev/Inertia` #48

Closed Shigetorum635 closed 2 years ago

Shigetorum635 commented 2 years ago
PS C:\Users\user\Development\Anomia\Anomia-Backend> node ace serve
[ info ]  building project...
[ info ]  starting http server...

IocLookupException: E_IOC_LOOKUP_FAILED: Cannot resolve "EidelLev/Inertia" namespace from the IoC Container

at C:\Users\user\Development\Anomia\Anomia-Backend\start\inertia.ts(anonymous):11
6   | Any code written inside this file will be executed during the application
7   | boot.
8   |
9   */
10
11  import Inertia from '@ioc:EidelLev/Inertia';
12  ^^^^^^^^^^^^^^^^^^^^^^^^^^

Here's where its highlited as the error origin. The rest is just some more info.

13  Inertia.share({
14    errors: (ctx) => {
15      return ctx.session.flashMessages.get('errors');
16    },

1 Function.lookupFailed
  C:\Users\user\Development\Anomia\Anomia-Backend\node_modules\@adonisjs\fold\build\src\Exceptions\IocLookupException.js:18

2 Ioc.lookupOrFail
  C:\Users\user\Development\Anomia\Anomia-Backend\node_modules\@adonisjs\fold\build\src\Ioc\index.js:254

3 Ioc.use
  C:\Users\user\Development\Anomia\Anomia-Backend\node_modules\@adonisjs\fold\build\src\Ioc\index.js:308

4 Module._compile
  C:\Users\user\Development\Anomia\Anomia-Backend\node_modules\pirates\lib\index.js:99

5 Object.newLoader [as .ts]
  C:\Users\user\Development\Anomia\Anomia-Backend\node_modules\pirates\lib\index.js:104

[ warn ]  Underlying HTTP server died with "0 code"
PS C:\Users\user\Development\Anomia\Anomia-Backend> 

Context:

I was making a REST API application, but figured that in my case it could be better to use InertiaJS, so I installed adonisjs session module aswell as InertiaJS, following the tutorials, and adding the middleware.

My use case is really simple;

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class AuthController {
  public async sendShit({ inertia }: HttpContextContract) {
    inertia.render('auth/auth', {
      ok: 'Jhon!',
    })
  }
}

For some reason VSCode can find the module, but Adonis is not capable.

Controller image start/inertia.ts image image

import Inertia from '@ioc:EidelLev/Inertia';

Inertia.share({
  errors: (ctx) => {
    return ctx.session.flashMessages.get('errors');
  },
}).version(() => Inertia.manifestFile('public/assets/manifest.json'));

I asked this on the Discord and they told me to check my providers in adonisrc.json file, it's there.

  "providers": [
    "./providers/AppProvider",
    "@adonisjs/core",
    "@adonisjs/lucid",
    "@adonisjs/auth",
    "adonisjs-hcaptcha",
    "@eidellev/inertia-adonisjs",
    "@adonisjs/session"
  ],
Shigetorum635 commented 2 years ago

Dearly and beloved member Julien did advise me!

image

Julien-R44 commented 2 years ago

For information, the problem is located here: https://github.com/eidellev/inertiajs-adonisjs/blob/90cb2d1c85088fc18fb5c28579edb15b9fe76aee/providers/InertiaProvider/InertiaProvider.ts#L174

If the user has not installed @adonisjs/view, the callback of withBindings will never be executed since Adonis/Core/View is not found, and therefore the provider of Inertia will never get registered, that's what causes the error

So you just have to run :

npm install @adonisjs/view
node ace configure @adonisjs/view

And you should be good 👍

Maybe @eidellev you could add a peerDependencies to @adonisjs/view or maybe a "Prerequisites" section in the readme ?

Shigetorum635 commented 2 years ago

For information, the problem is located here:

https://github.com/eidellev/inertiajs-adonisjs/blob/90cb2d1c85088fc18fb5c28579edb15b9fe76aee/providers/InertiaProvider/InertiaProvider.ts#L174

If the user has not installed @adonisjs/view, the callback of withBindings will never be executed since Adonis/Core/View is not found, and therefore the provider of Inertia will never get registered, that's what causes the error

So you just have to run :

npm install @adonisjs/view
node ace configure @adonisjs/view

And you should be good 👍

Maybe @eidellev you could add a peerDependencies to @adonisjs/view or maybe a "Prerequisites" section in the readme ?

Made a PR! https://github.com/eidellev/inertiajs-adonisjs/pull/49#issue-1096472131