Skip to main content
The Application class is the core of an AdonisJS application. It manages the application lifecycle, service providers, container bindings, and configuration. This module re-exports all functionality from @adonisjs/application.

Import

import { Application } from '@adonisjs/core/app'
import type { ApplicationService } from '@adonisjs/core/types'

ApplicationService Interface

The ApplicationService is a type-safe interface that extends the Application class with all registered container bindings.
interface ApplicationService extends Application<ContainerBindings> {}

Key Properties

environment

The current environment in which the application is running.
app.environment // 'web' | 'console' | 'test' | 'repl'
environment
AppEnvironments
Returns the current application environment.

appRoot

The root URL of the application.
app.appRoot // URL
appRoot
URL
Returns the application root as a URL object.

container

The IoC container instance for dependency injection and service resolution.
app.container
container
Container
Returns the container instance with all registered bindings.

config

The configuration store containing all application configuration.
app.config
config
ConfigProvider
Returns the configuration provider instance.

isReady

Indicates whether the application has been fully started.
app.isReady // boolean
isReady
boolean
Returns true if the application has completed the start phase.

isBooted

Indicates whether the application has been booted.
app.isBooted // boolean
isBooted
boolean
Returns true if the application has completed the boot phase.

Lifecycle Methods

init

Initializes the application by loading service providers and registering bindings.
await app.init()
return
Promise<void>
Returns a promise that resolves when initialization is complete.

boot

Boots all registered service providers.
await app.boot()
return
Promise<void>
Returns a promise that resolves when all providers have been booted.

start

Starts the application and triggers the ready hook of all providers.
await app.start(callback)
callback
() => void | Promise<void>
required
Callback function to execute during the start phase, after booting but before the ready hook.
return
Promise<void>
Returns a promise that resolves when the application is fully started.

terminate

Terminates the application gracefully by running all termination hooks.
await app.terminate()
return
Promise<void>
Returns a promise that resolves when the application has been terminated.

Lifecycle Hooks

booting

Register a callback to be executed during the booting phase.
app.booting(async () => {
  // Your booting logic
})
callback
() => void | Promise<void>
required
Callback function to execute during booting.

booted

Register a callback to be executed after the application has booted.
app.booted(async () => {
  // Your booted logic
})
callback
() => void | Promise<void>
required
Callback function to execute after booting.

starting

Register a callback to be executed during the starting phase.
app.starting(async () => {
  // Your starting logic
})
callback
() => void | Promise<void>
required
Callback function to execute during starting.

ready

Register a callback to be executed when the application is fully ready.
app.ready(async () => {
  // Your ready logic
})
callback
() => void | Promise<void>
required
Callback function to execute when ready.

terminating

Register a callback to be executed during termination.
app.terminating(async () => {
  // Your cleanup logic
})
callback
() => void | Promise<void>
required
Callback function to execute during termination.

Environment Management

setEnvironment

Sets the application environment.
app.setEnvironment('repl')
environment
AppEnvironments
required
The environment to set. Valid values: 'web', 'console', 'test', 'repl'.

inProduction

Check if the application is running in production mode.
if (app.inProduction) {
  // Production-specific logic
}
inProduction
boolean
Returns true if NODE_ENV is set to 'production'.

inDev

Check if the application is running in development mode.
if (app.inDev) {
  // Development-specific logic
}
inDev
boolean
Returns true if NODE_ENV is not set to 'production' or 'test'.

inTest

Check if the application is running in test mode.
if (app.inTest) {
  // Test-specific logic
}
inTest
boolean
Returns true if NODE_ENV is set to 'test'.

Notification

notify

Notifies the parent process with application status information.
app.notify(payload)
payload
object
required
Notification payload to send to the parent process.

Usage Examples

Basic Application Lifecycle

import { Application } from '@adonisjs/core/app'

const app = new Application(new URL('../', import.meta.url))

// Initialize
await app.init()

// Boot providers
await app.boot()

// Start application
await app.start(async () => {
  console.log('Application is starting')
})

// Terminate when done
await app.terminate()

Using Lifecycle Hooks

app.booting(() => {
  console.log('Application is booting')
})

app.ready(() => {
  console.log('Application is ready')
})

app.terminating(async () => {
  console.log('Cleaning up resources')
  // Close database connections, etc.
})

Accessing Container Services

// After the app is initialized and booted
const logger = await app.container.make('logger')
const router = await app.container.make('router')
const server = await app.container.make('server')

logger.info('Application started')

Environment-Specific Logic

if (app.inProduction) {
  // Enable production optimizations
  app.config.set('app.debug', false)
}

if (app.inDev) {
  // Enable development tools
  app.config.set('app.debug', true)
}

if (app.inTest) {
  // Use in-memory database
  app.config.set('database.connection', 'sqlite')
}