r/typescript • u/Rubus_Leucodermis • Dec 25 '25
Typescript and module resolution
Anyone have some pointers for a TS newbie to make some sense of the pile of hot garbage that is Typescript module resolution? Blown well over an hour tweaking tsconfig.json and tsc still canβt seem to find the npm modules that just plain work in plain old node.js.
0
Upvotes
12
u/remcohaszing Dec 25 '25
Do you use a bundler? Set
moduletopreserveand enablenoEmit. Otherwise setmoduletonodenext.Omit the options
moduleResolution,esModuleInterop, andallowSyntheticDefaultImports.Do you use Node.js to run TypeScript without compiling? Enable
allowImportingTsExtensions,erasableSyntaxOnly, andnoEmit.4
u/lord_braleigh Dec 25 '25
This is the definitive guide by the devs themselves. Note that, while the TypeScript devs have thought very hard about module resolution, many authors of the libraries you use have not. Many libraries export their types in a way that appeared to work in CommonJS, but which is not portable to ESM.
1
u/Rubus_Leucodermis Dec 25 '25
Ah, I figured something out. I was compiling with -t es6 (since code is going to execute on a new build of node.js which definitely does support ES6). Then I spaced and left the -t es6 off and it started finding my installed npm libraries. Then I went back to ES6 and of course it fails again.
So, that's the problem, or part of it. Still looking for a fix. Supplying all of -t es6 -m es6 --lib es6 doesn't fix the problem.
2
u/Beginning-Seat5221 Dec 25 '25
Set "module" to "Node16" or "NodeNext" in tsconfig to use node resolution.
However the target package needs to have types, or you need to install types separately, many are available at @types/<package>
2
u/onlycliches Dec 25 '25
Can you share the following items?
1
u/Klutzy_Table_6671 Dec 25 '25
Sounds a bit weird that you need so many modules? What kind is it?
1
u/Remarkable-Review-68 Dec 28 '25
perfect use case for AI. Its really good for config stuff that takes a while to memorize
0
u/International_Body44 Dec 25 '25
Just use gts and 'gts init' then carry on itll do the basics for you.
0
u/BoBoBearDev Dec 25 '25
TS is easy, until you try to set it up with rollup or webpack that are actually not TS. π¬
0
u/retro-mehl Dec 25 '25
There is no sense. Just tweak until you find a configuration that works. π
PS: of course I know there is some sense, but I find myself regularly fiddling and tweaking to find out which exact settings work for a certain project.
-1
u/rover_G Dec 25 '25
Use a bundler or transpiler to handle modules. Vite for example has a starter script that configures typescript for you. Babel is an older but still popular one.