0
votes

I am creating a NodeJS application where I need to create internal modules to better organize my code logic and avoid having to write the full path when referencing such modules.

internal-module.ts

export class A {
    test() {
  }
}

I have a bunch of files with exported classes in them, which are then all exported from one index file.

index.ts

export * from './internal-module'
export * from './internal-module2'

I am then generating a single definition file for all of these internal modules by using dts-generator.

index.d.ts

declare module 'src/internal-module' {
     export class A {
     test(): void;
   }
}

declare module 'src/index' {
    export * from 'src/internal-module';
    export * from 'src/internal-module2';
}

Then I'm consuming such module as follows:

consumer.ts

import {A} from "src/internal-module";

This all works from a Typescript's perspective - as in, I get intellisense after I generate the definition files... but then when running the actual NodeJS code (after also compiling the .ts files), the modules are not being found:

Error: Cannot find module 'src/internal-module'

I noticed that in the compiled .js file there's this code:

consumer.js

var a = require("src/internal-module");

Seems like this is the same syntax that NodeJS uses for external modules, which searches through node_modules folder for. Am I missing something? Is the issue related to the way I'm compiling TS?

I'm compiling TS with the suggested CommonJS mode:

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "declaration": true
    }
}
1

1 Answers

0
votes

Try with a relative path (prefixed by ./):

import {A} from "./src/internal-module";