TypeORM stops with Duplicate migrations on run or generate
Error: Duplicate migrations: User1655123435373Short 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.
Fix it fast
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 dist2Match what you see, then jump to its fix
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.ts2Fix
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.tsConfirmed by typeorm.io
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 -k42Fix
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/AddStatusColumn3Verify
Every class name is unique and the command runs.
grep -rhn "export class" src/migrations | sort | uniq -dConfirmed by typeorm.io
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeORM CLI says it is missing the dataSource argument
Missing required argument: dataSource
Since TypeORM 0.3 the CLI no longer reads ormconfig.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 causes, each with a fixBack end
TypeError: something.default is not a constructor in a NestJS app
Exception has occurred: TypeError: xxx.default is not a constructor
TypeScript turns import X from 'pkg' into require('pkg').default when esModuleInterop is off, and a CommonJS package has no default, so new X() fails.
2 causes, each with a fixBack end
TypeScript cannot find req.cookies in a NestJS controller
error TS2339: Property 'cookies' does not exist on type
TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed.
3 causes, each with a fixBack end
TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.
TypeORM replaced Connection with DataSource.
2 causes, each with a fix