Error ERR_UNSUPPORTED_DIR_IMPORT: Directory import x is not supported resolving ES modules
Closed this issue · 1 comments
milahu commented
typescript input
import { SomeThing } from './some-dir';
actual javascript output: in ESM, this throws ERR_UNSUPPORTED_DIR_IMPORT
import { SomeThing } from './some-dir';
expected javascript output: resolve directory import to file import
import { SomeThing } from './some-dir/index.js';
in commonjs, the directory import would work
const { SomeThing } = require ('./some-dir');
ideally, ts-to-jsdoc would support converting an input directory, so it can resolve file paths
related issues
- https://stackoverflow.com/questions/64453859/directory-import-is-not-supported-resolving-es-modules-with-node-js
- https://stackoverflow.com/questions/62619058/appending-js-extension-on-relative-import-statements-during-typescript-compilat
possible solutions
resolveFullPaths
option of tsc-alias- add
{ module: "nodenext" }
to compilerOptions to generate ESM javascript, then typescript requires file import and throwserror TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.
- https://github.com/antongolub/tsc-esm-fix
- to post-process outputs each time:
tsc-esm-fix --target='target/es6'
- to patch ts sources once:
tsc-esm-fix --src='src/main/ts' --ext='.js'
- to post-process outputs each time:
- https://github.com/antongolub/tsc-esm-fix
futurGH commented
Not planning on touching imports — I think I'm going to take the same stance as TS and say your imports in TS should be written the way you want them to appear in the output JS.