Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

The Ace console

Ace is the AdonisJS console. It is a set of commands run through node ace that do routine work: create controllers and models from templates, run migrations, install packages. It is the counterpart of Artisan in Laravel.

Listing commands

node ace list

The command prints all available commands grouped by topic: make, migration, db and others. The --help flag shows details of any command:

node ace make:controller --help

You see the command's arguments and flags with descriptions.

Creating files

The make commands create files in the right folder with the right name:

node ace make:controller posts

The file app/controllers/posts_controller.ts appears with an empty PostsController class. File names in AdonisJS are written in snake_case — words joined with underscores.

CommandWhat it creates
make:controller postsapp/controllers/posts_controller.ts
make:model Postapp/models/post.ts
make:migration postsa file in database/migrations/
make:validator postapp/validators/post.ts
make:middleware log_requestsa file in app/middleware/
make:view posts/indexresources/views/posts/index.edge

Installing packages

Official AdonisJS packages are installed with the add command. It installs the package through npm and configures it right away: creates a file in config/, adds variables to .env, registers a provider in adonisrc.ts.

node ace add @adonisjs/mail

If the package is already installed through npm, configure it with node ace configure @adonisjs/mail.

Your own command

You can write your own command too. Create it:

node ace make:command greet

Open the new file and add an argument and the run method:

// commands/greet.ts
import { BaseCommand, args } from '@adonisjs/core/ace'
 
export default class Greet extends BaseCommand {
  static commandName = 'greet'
  static description = 'Greet a user'
 
  @args.string()
  declare name: string
 
  async run() {
    this.logger.info(`Hello, ${this.name}!`)
  }
}

Now node ace greet Anna prints "Hello, Anna!" in the terminal. The commandName property sets the command name, and the @args.string() decorator declares an argument.

REPL

node ace repl opens an interactive console with the application loaded. It is handy for trying database queries without building pages.

Common mistake

make commands do not overwrite existing files: if posts_controller.ts already exists, Ace says so and creates nothing. Do not delete the file just to rerun the command — you will lose the code you wrote. If you need another file, give it another name.

Thanks for reading!

Donate to the site — we work hard for you.