Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM stops with Duplicate migrations on run or generate

ErrorError: Duplicate migrations: User1655123435373

Short answer

TypeORM collects every migration class that your migrations glob matches and refuses to continue when two of them have the same class name. The usual causes are a glob that matches both the .ts sources and the compiled .js files, or a migration file copied without renaming its class. Fix the glob or the class name.

  • Applies to TypeORM 0.3.x, Node.js 22, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Take the class name from the error and search for it in both the source folder and the build folder.

    grep -rl "User1655123435373" src dist
  2. 2Match what you see, then jump to its fix

    1. 1The name in the error appears in src/ and in dist/The glob loads the .ts source and the compiled .js of the same fileFix
    2. 2Two files contain the same class nameA migration file was copied and its class name was not changedFix

What does the error look like?

  • Error: Duplicate migrations: User1655123435373

Cause 1 of 2

The glob loads the .ts source and the compiled .js of the same file

1Check

The docs show a migrations glob ending in {.js,.ts} for development. After a build, that pattern can match the compiled copies as well, so each class is loaded twice.

grep -n "migrations" src/data-source.ts

2Fix

Make the glob match one file type per environment: .ts when you run the CLI from source, .js from the build folder at runtime.

const isCli = process.env.CLI === "typeorm"
const baseDir = isCli ? "src" : "dist"
const ext = isCli ? "ts" : "js"

export const AppDataSource = new DataSource({
  // ...
  migrations: [`${baseDir}/migrations/*.${ext}`],
})

3Verify

Run the command again; the duplicate error is gone.

npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts

Confirmed by typeorm.io

Did this fix it?

Cause 2 of 2

A migration file was copied and its class name was not changed

1Check

TypeORM's duplicate check compares the class names of all loaded migrations. A copied file keeps the old name until you edit it.

grep -rn "export class" src/migrations | sort -k4

2Fix

Delete the copy, or regenerate it so it gets a new timestamp in the file name and class name. Do not rename a migration that has already run in a shared database.

npx typeorm-ts-node-commonjs migration:create ./src/migrations/AddStatusColumn

3Verify

Every class name is unique and the command runs.

grep -rhn "export class" src/migrations | sort | uniq -d

Confirmed by typeorm.io

Did this fix it?

How do I stop it coming back?

  • Create each migration with the CLI so the timestamp is part of the class name.
  • Run the CLI against src and the app against dist, with separate globs.
  • Review migration files in pull requests for copied class names.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.