Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript export sub-modules without "dist" folder

I have a simple module modA:

modA/
- package.json
- dist/
  - index.js
  - db.js
  - stuff.js

I'd like to be able to use the submodules "db" and "stuff" like this: import * as db from modA/db -- how can I do that? I have main: dist/index.js in my package.json but that doesn't set dist/ as a default for submodules, so the only way I can get it to work is import * as db from modA/dist/db (explictly including the "dist" in the import). import * as db from modA/db just gives the "Cannot find module" error.

The dist is there because I'm compiling from typescript.

In case it's important, I want this to work in node.js and browser, where I'm using webpack.

As an alternative, can I add some kind of namespace re-export code in index.js to make this work?

like image 378
GaryO Avatar asked Feb 06 '26 20:02

GaryO


1 Answers

In order to make following import work

import * as db from 'modA/db';

In tsconfig.json add paths like below

 {
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "modA/*": [
        "dist/*"
      ]
    }
  }
} 

Ref: https://indepth.dev/configuring-typescript-compiler/

like image 137
M A Salman Avatar answered Feb 09 '26 12:02

M A Salman