Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated TypeScript Models Lack File Extensions in Import Statements When Using Node 16 Module Resolution #296

Open
sfunke opened this issue Oct 25, 2023 · 2 comments

Comments

@sfunke
Copy link

sfunke commented Oct 25, 2023

Hello,

first, thanks for this library and the efforts to maintain it!

I've encountered an issue using it in a TypeScript project with Node 16 and moduleResolution set to node16 (or nodenext) in the tsconfig.json.

When the TypeScript models are generated, the import statements in the generated files lack the .js extension for relative file paths. This is problematic because Node 16 requires explicit file extensions in ES module imports: https://nodejs.org/api/esm.html#mandatory-file-extensions

Example Error:

models/contentful-schemes/TypeDiscoverLandingPage.ts:3:48 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './TypeBrandCarousel.js'?

Incorrect Import Statement:

export { isTypeArticleCarousel } from "./TypeArticleCarousel";

Expected Import Statement:

export { isTypeArticleCarousel } from "./TypeArticleCarousel.js";

tsconfig.json Configuration:

{
    "module": "Node16",
    "moduleResolution": "node16"
}

Questions and Offer to Contribute:

  • Might this be a configurational problem on my end? Unfortunately, I am not able to change module or moduleResolution in my project, but maybe I missed a flag in the library to configure the module compatibility?
  • Is this a known issue? If so, is there a planned fix?
  • I'd like to contribute a fix for this issue. For that, could you guide me to the part of the codebase where the import statements are generated?

Thank you!

@marcolink
Copy link
Collaborator

Still waiting for an answer here

@danitt
Copy link

danitt commented Oct 29, 2024

had the same issue, solved it using the moduleSpecifier, e.g.

  const moduleSpecifier = outputFile.getRelativePathAsModuleSpecifierTo(inputFile.getFilePath()) + '.js';

  outputFile.addImportDeclaration({
    moduleSpecifier,
    ...
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants