Search code examples
nestjsproject-structure

How can I fix the issue: "Cannot find module '.todo/todo' or its corresponding type declaration" while specifying relative path to all files?


I'm working with the NestJS project following the next article (self-learning purposes).

I have the project-structure similar to:

project
...
src
   |-- app.controller.spec.ts
   |-- app.controller.ts
   |-- app.module.ts
   |-- app.service.ts
   |-- config
   |   |-- database.config.ts
   |-- main.ts
   |-- todo
   |   |-- todo.controller.ts
   |   |-- todo.entity.ts
   |   |-- todo.module.ts
   |   |-- todo.service.ts

where I have specifically the folder which contains all todo files inside and I need to specify manually relative import to this todo folder:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Todos } from './todo/todo';

@Module({
  imports: [Todos],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

the problem is when I'm trying to do it, I'm getting the issue as:

TS2307: Cannot find module './todo/todo' or its corresponding type declaration.

enter image description here

Based on it, my question is:

how can I specify inside corresponding file app.module.ts relative path using import to this folder including all files?

I've already checked documentation Nest Docs and Nest CLI. Also I've tried to use commands similar to:

npx nest g module/controller/provider

which will handle the imports, but I need to find the way how to specify import with relative path including all files.

Can I avoid the usage of any custom index.ts files at the route using commands like:

export * from './path/to/file/to/export/from

just specifying relative path inside import for the file app.module.ts?

I appreciate any ideas here.


Solution

  • You need to make an index.ts file at the route where you want the barrel to be, then you use export * from './path/to/file/to/export/from as many times as necessary to export all of the things you want to export. Then you can use import { thing } from '../path/to/<directory> instead of specifying each file