adminjs-nestjs icon indicating copy to clipboard operation
adminjs-nestjs copied to clipboard

Can not import after update to 6.0.1

Open EliasGcf opened this issue 1 year ago • 8 comments

I am getting this error when update to 6.0.1 version.

image

EliasGcf avatar Apr 19 '23 23:04 EliasGcf

I have the same problem

conneryfrommi6 avatar Apr 28 '23 16:04 conneryfrommi6

Same problem over here.

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

pedro-lb avatar Apr 28 '23 20:04 pedro-lb

Same problem, did someone fix this?

Yurichz avatar May 11 '23 10:05 Yurichz

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

synic avatar May 11 '23 19:05 synic

@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.

nodeteamdev avatar Jun 20 '23 06:06 nodeteamdev

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 {}

thefelixno avatar Aug 15 '23 10:08 thefelixno

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

ZeroCho avatar Sep 06 '23 05:09 ZeroCho

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

niksbanna avatar Nov 04 '23 04:11 niksbanna