LogoLogo
Join our community
  • AdminJS
  • Contribute
  • Demo
  • Addons Marketplace
  • Installation
    • Getting started
    • Plugins
      • Adonis
      • Express
      • Nest
      • Fastify
      • Hapi
      • Koa
      • Community Plugins
        • FeathersJS
        • AdonisJS
      • Matrix
    • Adapters
      • TypeORM
      • Sequelize
      • Prisma
      • MikroORM
      • Objection
      • SQL
      • Mongoose
      • Community Adapters
        • AdonisJS
    • What's new in v7?
    • Migration Guide v7
  • Basics
    • Resource
    • Action
    • Property
    • Features
      • Relations
      • Upload
      • Logger
      • Import & Export
      • Password
      • Leaflet Maps
      • Writing your own features
    • API
      • List
      • New
      • Search
      • Show
      • Edit
      • Delete
      • Bulk Delete
    • Themes
    • Authentication
      • FirebaseAuthProvider
      • MatrixAuthProvider
  • How to write an addon?
  • UI Customization
    • Writing your own Components
    • Overwriting CSS styles
    • Dashboard customization
    • Changing the form view
    • Storybook
  • Tutorials
    • Role-Based Access Control
    • Internationalization (i18n)
    • Content Management System
    • Custom components library
    • Custom component internationalization
  • FAQ
    • PDF Generator
    • Charts
    • Forgot Password
  • ⚠️Legacy documentation
Powered by GitBook
On this page
  • How to remove authentication?
  • Using AdminJS ESM packages
  1. Installation
  2. Plugins

Nest

@adminjs/nestjs

PreviousExpressNextFastify

Last updated 1 year ago

AdminJS is now ESM-only. If you plan to use NestJS in your project, NestJS doesn't work with ESM out of the box and you should be aware that you can run into issues. We suggest to use @adminjs/express instead. This guide will show you how you can set up the latest AdminJS version with NestJS but do note that this is just a workaround until NestJS maintainers decide to move to or at least support ESM.

Example repository with AdminJS v7 working with NestJS & ESM:

Make sure you have installed AdminJS packages described in article.

$ yarn add adminjs @adminjs/nestjs

As of version 5.x.x of @adminjs/nestjs, the plugin only supports Nest servers that use Express. Fastify Nest servers are not currently supported.

If you are starting a new project, install nest-cli and bootstrap the project:

$ npm i -g @nestjs/cli
$ nest new

@adminjs/nestjs uses @adminjs/express to set up AdminJS, because of this you have to additionally install it's dependencies:

$ yarn add @adminjs/express express-session express-formidable

Set moduleResolution to nodenext or node16 in your tsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "node16",
    "module": "commonjs",
    "target": "esnext",
    // ...
  }
}

Next, add Nest plugin code into imports of your AppModule:

app.module.ts
import { Module } from '@nestjs/common'

import { AppController } from './app.controller'
import { AppService } from './app.service'

const DEFAULT_ADMIN = {
  email: 'admin@example.com',
  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: [
    // AdminJS version 7 is ESM-only. In order to import it, you have to use dynamic imports.
    import('@adminjs/nestjs').then(({ AdminModule }) => AdminModule.createAdminAsync({
      useFactory: () => ({
        adminJsOptions: {
          rootPath: '/admin',
          resources: [],
        },
        auth: {
          authenticate,
          cookieName: 'adminjs',
          cookiePassword: 'secret'
        },
        sessionOptions: {
          resave: true,
          saveUninitialized: true,
          secret: 'secret'
        },
      }),
    })),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Now you should be able to start your AdminJS application:

$ nest start

AdminJS panel will be available under http://localhost:3000/admin if you have followed this example thoroughly.

How to remove authentication?

If you don't want your admin panel to require authentication, simply follow the example above, but remove auth and sessionOptions configuration, for example:

import('@adminjs/nestjs').then(({ AdminModule }) => AdminModule.createAdminAsync({
  useFactory: () => ({
    adminJsOptions: {
      rootPath: '/admin',
      resources: [],
    },
  }),
})),

Using AdminJS ESM packages

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');
https://github.com/dziraf/adminjs-v7-with-nestjs
Getting started
Page cover image