Skip to content
English
  • There are no suggestions because the search field is empty.

[Public] Resolving Errors with verbatimModuleSyntax while using IronPdf for Node.js

Understanding verbatimModuleSyntax in TypeScript and How It Affects Module Imports

When working with TypeScript, module imports and exports need to align with the specified module type. One common issue developers face is related to verbatimModuleSyntax, a TypeScript compiler option that controls how import and export statements are handled in the output.

Setting "verbatimModuleSyntax": false can help resolve errors by allowing TypeScript to automatically adjust import and export statements based on the target module type (e.g., CommonJS or ESM).

When Set to false (Default Behavior)

  • TypeScript automatically transforms import/export statements to match the specified module system.
  • If the target module system is CommonJS, TypeScript converts ESM-style imports (import ... from '...') into require() statements.
  • If the target is ESNext, TypeScript keeps the ESM syntax.

When Set to true (Strict Mode)

  • TypeScript preserves the original import/export syntax exactly as written in the source file.
  • If an ESM-style module is written, the output remains in ESM format, regardless of whether the target module system is CommonJS or another type.
  • This can lead to errors when the module system does not support ESM syntax.

Common Errors When verbatimModuleSyntax is Enabled

When verbatimModuleSyntax is set to true, developers may encounter errors like:

  • "A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled."

    • This happens because CommonJS does not support export at the top level.
  • "ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled."

    • Occurs when TypeScript does not transform the ESM syntax into CommonJS syntax, causing compatibility issues.

When to Use verbatimModuleSyntax

  • If your project uses both ESM and CommonJS and needs automatic adjustments, keep "verbatimModuleSyntax": false.
  • If you need strict module preservation (e.g., for specific bundlers or environments), set "verbatimModuleSyntax": true, but ensure your module system supports it.

Conclusion

Understanding how verbatimModuleSyntax works is essential when configuring TypeScript for different module formats. In most cases, keeping it disabled (false) prevents compatibility issues by allowing TypeScript to adjust module syntax automatically. However, for projects requiring strict syntax preservation, enabling it (true) requires careful handling to avoid module system conflicts.