adminjs-nestjs
adminjs-nestjs copied to clipboard
Can not import after update to 6.0.1
I am getting this error when update to 6.0.1 version.
I have the same problem
Same problem over here.
AdminJS is at version 7 but this dependency is at version 6, are we outdated?
Same problem, did someone fix this?
Same issue here - @dziraf any insight? The same seems to be true for @adminjs/passwords, 4.0.0 is not importable.
@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.
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 Promise.all
can be better for readability. :)
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