Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not import after update to 6.0.1 #55

Open
EliasGcf opened this issue Apr 19, 2023 · 8 comments
Open

Can not import after update to 6.0.1 #55

EliasGcf opened this issue Apr 19, 2023 · 8 comments

Comments

@EliasGcf
Copy link

I am getting this error when update to 6.0.1 version.

image

@conneryfrommi6
Copy link

I have the same problem

@pedro-lb
Copy link

Same problem over here.

AdminJS is at version 7 but this dependency is at version 6, are we outdated?

@Yurichz
Copy link

Yurichz commented May 11, 2023

Same problem, did someone fix this?

@synic
Copy link

synic commented May 11, 2023

Same issue here - @dziraf any insight? The same seems to be true for @adminjs/passwords, 4.0.0 is not importable.

@nodeteamdev
Copy link

nodeteamdev commented Jun 20, 2023

@EliasGcf Since AdminJS version 7 and it's compatible packages are ESM-only, you cannot import them directly into your CommonJS Nest app. Instead, you have to use dynamic imports:

const adminjsUploadFeature = await import('@adminjs/upload');

from official documentation, https://docs.adminjs.co/installation/plugins/nest

p.s. But after that update, I do not able to use it on my current projects... I have so many issues with my Nest.js app after upgrade.

@thefelixno
Copy link

combining adminjs into one big dynamic import module solved it for me:

@Module({
  imports: [
    // AdminJS version 7 is ESM-only. In order to import it, you have to use dynamic imports.
    import('@adminjs/nestjs').then(({ AdminModule }) => {
      return import('adminjs').then(({ AdminJS }) => {
        return import('@adminjs/typeorm').then((AdminJSTypeORM) => {
          AdminJS.registerAdapter({ Database: AdminJSTypeORM.Database, Resource: AdminJSTypeORM.Resource });

          return AdminModule.createAdminAsync({
            useFactory: () => ({
              adminJsOptions: {
                rootPath: '/admin',
                resources: [Appointment, Availability, Event, Reply, ResourceEntity],
              },
            }),
          });
        });
      });
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

@ZeroCho
Copy link

ZeroCho commented Sep 6, 2023

@thefelixno Promise.all can be better for readability. :)

@niksbanna
Copy link

niksbanna commented Nov 4, 2023

AdminJS version 7 is ESM-only. In order to import it, you have to use dynamic imports.
/* app.module.ts */

`import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { ConfigModule } from '@nestjs/config'
import { MongooseModule, getModelToken } from '@nestjs/mongoose'
import { User } from './mongoose/user.entity'
import { MongooseSchemasModule } from './mongoose/mongoose.module'
import { Admin } from './mongoose/admin-model'
import { Model } from 'mongoose'


import('adminjs').then(({AdminJS}) => {
  import('@adminjs/mongoose').then((AdminJSMongoose) => {
    AdminJS.registerAdapter({
      Resource: AdminJSMongoose.Resource,
      Database: AdminJSMongoose.Database,
    });
  });
});

const DEFAULT_ADMIN = {
  email: '[email protected]',
  password: 'password',
}

const authenticate = async (email: string, password: string) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN)
  }
  return null
}

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    MongooseModule.forRoot(process.env.DB_URL),
    import('@adminjs/nestjs').then(({ AdminModule }) => AdminModule.createAdminAsync({
      imports: [
        MongooseSchemasModule
      ],
      inject: [
        getModelToken('Admin'),
        getModelToken('User'),
      ],
      useFactory: (adminModel: Model<Admin>, userModel: Model<User>) => ({
        adminJsOptions: {
          rootPath: '/admin',
          resources: [
            { resource: adminModel },
            {
              resource: userModel,
              options: {
                sort: {
                  sortBy: 'last_login',
                  direction: 'desc',
                },
              },
            },
          ],
        },
        auth: {
          authenticate,
          cookieName: 'adminjs',
          cookiePassword: 'secret'
        },
        sessionOptions: {
          resave: true,
          saveUninitialized: true,
          secret: 'secret'
        },
      }),
    })),
    MongooseSchemasModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }`

this works fine for me, hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants