how to specify ormconfig.ts for typeorm?

Just remove the default while exporting. Your ormconfig.ts should be something like:

import env from './src/env';

export = {
  type: 'mysql',
  port: env.DB_CONFIG.port,
  username: env.DB_CONFIG.username,
  password: env.DB_CONFIG.password,
  database: env.DB_CONFIG.database,
  entities: [
  migrations: [
  cli: {
    migrationsDir: 'src/database/migrations',
  synchronize: false,

In my case I'm using a main env.ts file, as the database connection needs to be different depending on the environment. Also, don't forget using ts-node for dealing with typeorm cli in package.json:

"scripts": {
    "migrate:create": "ts-node ./node_modules/typeorm/cli.js migration:create -n",
    "migrate:up": "ts-node ./node_modules/typeorm/cli.js migration:run",
    "migrate:down": "ts-node ./node_modules/typeorm/cli.js migration:revert"

So creating, running or rolling back migrations should be like:

npm run migrate:create FileName
npm run migrate:up
npm run migrate:down

Hey i up this conversation since i can propose you a solution.

You can put the following line in your package.json file:

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config server/environments/database.ts",

And your ts config must export directly the config by doing that:

export = { /* your config */ };

As you can see, you can also specify the path of your config. No need for your config to be at the root level of your project.

Hope that will help you

Project Structure

├── src // Typescript files
│   ├── entities
│   │   └── User.ts
│   ├── db
│   │   └── ormconfig.ts
│   │   ├── migrations
│   │   │   └── ... // migration files
├── tsconfig.json
├── package.json


  • Typeorm should be able to use src/db/ormconfig.ts file for connection.
  • We should be able to create migrations, and perform all typeorm cli supported operations using src/db/ormconfig.ts file.


We might be able to accomplish this using ts-node package available for typescript.

Solution and Github Link

Please checkout for complete example.

Contents of src/db/ormconfig.ts file are:

import path from "path";
import { ConnectionOptions } from "typeorm";

export default {
  name: "default",
  type: "better-sqlite3",
  database: ":memory:",
  synchronize: true,
  migrationsRun: true,
  dropSchema: false,
  entities: [path.join(__dirname, "..", "entities", "**", "*.*"), path.join(__dirname, "..", "entities", "*.*")],
  migrations: [path.join(__dirname, "migrations", "*.*")],
  cli: {
    entitiesDir: path.join(__dirname, "..", "entities"),
    migrationsDir: path.join(__dirname, "migrations")
} as ConnectionOptions;

Script section of src/db/package.json file are:

"scripts": {
  "dev": "ts-node-dev src/index.ts",
  "build": "tsc",
  "start": "node dist/index.js",
  "typeorm": "ts-node ./node_modules/.bin/typeorm -f ./src/db/ormconfig.ts",
  "migration:generate": "yarn run typeorm migration:generate -n",
  "migration:blank": "yarn run typeorm migration:create -n"

## Usage

# Generate a blank migration
yarn migration:blank migration-name-here

# Generate migrations from database and entities.
yarn migration:generate

# Roll back a migration using cli options.
yarn typeorm migration:down

At the time of writing, TypeORM only look for ormconfig.json and ignores ormconfig.ts. There is work in progress to support it though.

Beside having ormconfig.json you need these commands in your package.json.

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm",
"migration:generate": "npm run typeorm -- migration:generate --config src/config/ormconfig.json --connection  --name ",
"migration:run": "npm run typeorm -- migration:run"