TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.Short answer
TypeORM replaced Connection with DataSource. createConnection and the global helpers such as getConnection and getRepository are deprecated, and version 1.0 removes them. Create one DataSource, call initialize() once, export it, and take repositories from that object.
Fix it fast
1Find out which cause you have
List every use of the old global functions so you can replace them in one pass.
grep -rnE "createConnection|getConnection|getRepository|getManager" src2Match what you see, then jump to its fix
What does the error look like?
'createConnection' is deprecated.
Cause 1 of 2
The connection is still opened with createConnection
1Check
The 0.3.0 release notes say createConnection and createConnections are deprecated because Connection is now called DataSource.
grep -rn "createConnection" src2Fix
Build a DataSource from the same options and initialize it once. The docs say initialize must only be called once; a second call rejects with CannotConnectAlreadyConnectedError.
// data-source.ts
import { DataSource } from "typeorm";
export const AppDataSource = new DataSource({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
});
// main.ts
await AppDataSource.initialize();3Verify
Start the app and watch for the connection to open without the hint in the editor.
npx tsc --noEmitConfirmed by github.com
Cause 2 of 2
Code elsewhere calls getConnection, getRepository or getManager
1Check
The 0.3.0 notes deprecate these global helpers and say to use a globally accessible data source instead. The 1.0 upgrade guide says they were removed.
grep -rnE "getRepository|getConnection|getManager" src2Fix
Import the exported data source and ask it for the repository. Replace close() with destroy() and connect() with initialize().
import { AppDataSource } from "./data-source";
const repo = AppDataSource.getRepository(User);
await AppDataSource.destroy();3Verify
Compile the project and run the code path that reads a user.
npx tsc --noEmitConfirmed by typeorm.io
How do I stop it coming back?
- Keep the DataSource in one module and import it, rather than creating a second one in a test or a script.
- Run tsc in CI so removed APIs fail the build at upgrade time.
- Read the upgrade guide before moving to a new major version.
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
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 stops with Duplicate migrations on run or generate
Error: Duplicate migrations: User1655123435373
TypeORM collects every migration class that your migrations glob matches and refuses to continue when two of them have the same class name.
2 causes, each with a fixBack end
TypeORM cannot find an entity, or the metadata for a relation
Entity metadata for Role#users was not found.
TypeORM only knows the entities listed in the entities option of the DataSource that is running.
2 causes, each with a fix