Maaz Zindani

Loading the index…

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

NestJS with TypeORM says Connection "default" was not found

ErrorConnection "default" was not found

Short 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().

  • Applies to NestJS 10, NestJS 11, TypeORM 0.3, @nestjs/typeorm 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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" src
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace points at a line with getConnection(), getManager() or getRepository(Entity)Code still calls the global getConnection() or getRepository()Fix
    2. 2You 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. 3The error comes from a seed or migration script run outside the Nest containerA script or CLI builds a DataSource but never initializes itFix

What does the error look like?

  • Connection "default" was not found
  • getConnection() 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\(" src

2Fix

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:dev

Confirmed by raw.githubusercontent.com

Did this fix it?

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" src

2Fix

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:dev

Confirmed by docs.nestjs.com

Did this fix it?

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/null

2Fix

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.js

Confirmed by typeorm.io

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

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