YousefED / typescript-json-schema

Generate json-schema from your Typescript sources
BSD 3-Clause "New" or "Revised" License
3.17k stars 323 forks source link

"Cannot find module" in interface that uses imports #507

Closed marcelo-agil closed 2 years ago

marcelo-agil commented 2 years ago

Hi Guys, I am programatically generating an schema based on an interface, that is builded from several imports, a quick example:

Interface file

import * as IUtil from 'modules/shared/util/util.interface';

export interface IPerson extends IUtil.IDateFields {
  id: string;
}

The programatically test:

program = typescriptJsonSchema.getProgramFromFiles(
      [resolve('path-to/person.interface.ts')],
      typescriptJsonSchemaCompilerOptions,
      '/var/www/crm-backend/src/'
    );

    fileSchema = typescriptJsonSchema.generateSchema(
      program,
      '*',
      typescriptJsonSchemaCompilerOptions
    );

    console.log(fileSchema);

"'/var/www/crm-backend/src/" is being totally ignored, because when code runs, throws this error: Cannot find module 'modules/shared/util/util.interface' or its corresponding type declarations.

The code runs perfectly, the file path is correct, everything is fine.

Heres my tsconfig.ts:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["es6"],
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": ".",
    "removeComments": true,
    "typeRoots": ["./node_modules/@types", "./src/@types"],
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "./src",
    "types": ["folder-inside-src/@types"],
    "paths": {
      "@sharedDomain/*": ["modules/shared/domain/*"],
      "@negotiation/*": ["modules/user/*"],
      "@notification/*": ["modules/notification/*"],
      "@customer/*": ["modules/customer/*"],
      "@person/*": ["shared/person/*"],
      "@personDomain/*": ["modules/shared/person/domain/*"],
      "@personController/*": ["modules/shared/person/app/http/controller/*"],
      "@personHelper/*": ["modules/user/app/http/helper/*"],
      "@security/*": ["modules/shared/security/*"],
      "@userDomain/*": ["modules/shared/security/domain/user/*"],
      "@product/*": ["modules/product/*"],
      "@scheduler/*": ["modules/scheduler/*"],
      "@shared/*": ["modules/shared/*"],
      "@config/*": ["config/*"],
      "@util/*": ["modules/shared/util/"]
    }
  },
  "include": ["src/**/*", "server.ts"]
}

It seems TJS is not getting the full correct path from these imports.

marcelo-agil commented 2 years ago

Found the solution:

Inside the TJSCompilerOptions insert the baseUrl param:, baseUrl: '/full-path-to-source/',