NestJS with TypeORM says Connection "default" was not found
Connection "default" was not foundShort answer
TypeORM could not find a data source under the name default. Since 0.3, Connection is DataSource and the global helpers such as getConnection() and getRepository() are deprecated. In NestJS, inject DataSource or the repository instead. Also check that a named data source is requested by its name and that standalone scripts call initialize().
Fix it fast
1Find out which cause you have
Search the code for the old global helpers, and see how many data sources are registered.
grep -rnE "getConnection|getRepository\(|getManager|createConnection" src grep -rn "TypeOrmModule.forRoot" src2Match what you see, then jump to its fix
- 1
The stack trace points at a line with getConnection(), getManager() or getRepository(Entity)Code still calls the global getConnection() or getRepository()Fix - 2
You registered a second connection with a name, and the failing code uses the default oneA named data source is requested without its name, or the reverseFix - 3
The error comes from a seed or migration script run outside the Nest containerA script or CLI builds a DataSource but never initializes itFix
- 1
What does the error look like?
Connection "default" was not foundgetConnection() or getRepository() fails in a service that worked on TypeORM 0.2
Cause 1 of 3
Code still calls the global getConnection() or getRepository()
1Check
The TypeORM changelog for 0.3.0 renames Connection to DataSource and deprecates createConnection(), getConnection(), getManager() and getRepository(); the deprecated signatures are to be removed in 0.4.0.
grep -rnE "getConnection|getManager|getRepository\(" src2Fix
Inject the DataSource (or the repository) that Nest creates, instead of asking a global registry.
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { DataSource, Repository } from 'typeorm'
import { User } from './user.entity'
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User) private readonly users: Repository<User>,
private readonly dataSource: DataSource,
) {}
}3Verify
Start the app; the error is gone and the query returns rows.
npm run start:devConfirmed by raw.githubusercontent.com
Cause 2 of 3
A named data source is requested without its name, or the reverse
1Check
A data source without a name is called default. Extra data sources need unique names, and the same name goes to forFeature() and the injection decorators.
grep -rnE "name:|forFeature\(|InjectDataSource" src2Fix
Give each data source a unique name and pass the name wherever it is used.
TypeOrmModule.forFeature([Album], 'albumsConnection')
constructor(
@InjectDataSource('albumsConnection')
private readonly albumsDataSource: DataSource,
) {}3Verify
Run the request that failed against each database.
npm run start:devConfirmed by docs.nestjs.com
Cause 3 of 3
A script or CLI builds a DataSource but never initializes it
1Check
Outside Nest, nothing connects for you. TypeORM says database access works only after DataSource.initialize() has been called, once.
grep -rn "new DataSource\|initialize()" src scripts 2>/dev/null2Fix
Call initialize() once at start-up and destroy() when the script ends.
const ds = new DataSource(options)
await ds.initialize()
// work with ds.getRepository(User) here
await ds.destroy()3Verify
Run the script and check that it connects and exits.
node dist/scripts/seed.jsConfirmed by typeorm.io
How do I stop it coming back?
- Do not use the deprecated global functions in new code; inject DataSource or repositories.
- Give every extra data source a name and keep a single place where the names are defined.
- When upgrading TypeORM, read the changelog deprecation list and grep for each removed helper.
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
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack end
NestJS GraphQL cannot work out the output type of a field or query
Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.
In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.
2 causes, each with a fixBack end
IntelliJ IDEA breakpoints are ignored in a NestJS app
No 'Debugger listening' line appears when the NestJS app starts
IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.
4 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack end
NestJS watch mode does not restart after I save a file
Changes show on the host but not in the running container
Watch mode only sees files that exist inside the process that runs it.
3 causes, each with a fix