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?
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With