Nest can't resolve a dependency such as DataSource or a repository
Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument DataSource at index [0] is available in the UsersModule context.Short answer
Nest builds each module on its own. The error means the module that owns the failing class has no provider for the token in the message. Turn on NEST_DEBUG, read the module and index it names, then register the provider, import the module that exports it, or export it from where it lives.
Fix it fast
1Find out which cause you have
Run the app with Nest's dependency logging on, and read the module name and index in the message. They tell you which module needs the fix.
NEST_DEBUG=1 npm run start2Match what you see, then jump to its fix
- 1
Please make sure that the argument <name> at index [<n>] is available in the <Module> moduleThe provider is not registered or not exported in a module the failing class can seeFix - 2
Nest reports that the class cannot appear in the imports arrayA class is listed in imports instead of providersFix - 3
The message has no token name: argument at index [0] is available in the current moduleThe token is undefined at runtime because two files import each otherFix - 4
The message has no token name, and the constructor parameter is an interface or imported with import typeAn interface or a type-only import is used as the injection tokenFix
- 1
What does the error look like?
Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument DataSource at index [0] is available in the UsersModule context.Please make sure that the argument at index [0] is available in the current module.
Cause 1 of 4
The provider is not registered or not exported in a module the failing class can see
1Check
The token must be in the module's providers, or exported by a module listed in its imports. For a TypeORM repository, the module needs TypeOrmModule.forFeature with the entity.
grep -rn "forFeature\|providers:\|exports:" src --include=*.module.ts2Fix
Register the repository in the module that injects it. To share it with other modules, export what forFeature generates and import that module there.
@Module({
imports: [TypeOrmModule.forFeature([User])],
providers: [UsersService],
exports: [TypeOrmModule],
})
export class UsersModule {}3Verify
The app boots without the resolution error.
npm run startConfirmed by docs.nestjs.com
Cause 2 of 4
A class is listed in imports instead of providers
1Check
Search the failing module for an @Injectable class inside its imports array.
grep -n -A6 "imports:" src/users/users.module.ts2Fix
Move the class to providers. Only modules belong in imports.
@Module({
imports: [TypeOrmModule.forFeature([User])],
providers: [UsersService],
})
export class UsersModule {}3Verify
Start the app again.
npm run startConfirmed by docs.nestjs.com
Cause 3 of 4
The token is undefined at runtime because two files import each other
1Check
Two files import each other, for example a module file that exports a constant a service imports. Barrel index.ts files can create the same loop.
npx madge --circular --extensions ts src2Fix
Break the import loop. Move shared constants or tokens into a separate file that imports nothing from the modules.
// src/users/users.constants.ts
export const USERS_TOKEN = 'USERS_TOKEN';3Verify
The check reports no cycle and the app starts.
npx madge --circular --extensions ts src
npm run startConfirmed by docs.nestjs.com
Cause 4 of 4
An interface or a type-only import is used as the injection token
1Check
Interfaces do not exist at runtime, and import type removes the class. Nest sees undefined or Object.
grep -rn "import type" src2Fix
Inject the class itself with a normal import, or use a string, Symbol or abstract class token with @Inject().
constructor(@Inject('CONNECTION') private connection: Connection) {}3Verify
The app starts and the service receives its dependency.
npm run startConfirmed by docs.nestjs.com
How do I stop it coming back?
- Give every feature module its own forFeature call and export only what other modules need.
- Keep tokens and constants in files that do not import modules or services.
- Run the app in CI so a missing provider fails the build, not production.
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