> ## Documentation Index
> Fetch the complete documentation index at: https://codegeninc-tawsif-change-additional-tools-to-tools.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Converting Default Exports

> Convert default exports to named exports in your TypeScript codebase

Codegen provides tools to help you migrate away from default exports to named exports in your TypeScript codebase. This tutorial builds on the concepts covered in [exports](/building-with-codegen/exports) to show you how to automate this conversion process.

## Overview

Default exports can make code harder to maintain and refactor. Converting them to named exports provides several benefits:

* Better IDE support for imports and refactoring
* More explicit and consistent import statements
* Easier to track symbol usage across the codebase

## Converting Default Exports

Here's how to convert default exports to named exports:

```python
for file in codebase.files:
    target_file = file.filepath
    if not target_file:
        print(f"⚠️ Target file not found: {filepath}")
        continue

    # Get corresponding non-shared file
    non_shared_path = target_file.filepath.replace('/shared/', '/')
    if not codebase.has_file(non_shared_path):
        print(f"⚠️ No matching non-shared file for: {filepath}")
        continue

    non_shared_file = codebase.get_file(non_shared_path)
    print(f"📄 Processing {target_file.filepath}")

    # Process individual exports
    for export in target_file.exports:
        # Handle default exports
        if export.is_reexport() and export.is_default_export():
            print(f"  🔄 Converting default export '{export.name}'")
            default_export = next((e for e in non_shared_file.default_exports), None)
            if default_export:
                default_export.make_non_default()

    print(f"✨ Fixed exports in {target_file.filepath}")
```

## Understanding the Process

Let's break down how this works:

<AccordionGroup>
  <Accordion title="Finding Default Exports">
    ```python
    # Process individual exports
    for export in target_file.exports:
        # Handle default exports
        if export.is_reexport() and export.is_default_export():
            print(f"  🔄 Converting default export '{export.name}'")
    ```

    The code identifies default exports by checking:

    1. If it's a re-export (`is_reexport()`)
    2. If it's a default export (`is_default_export()`)
  </Accordion>

  <Accordion title="Converting to Named Exports">
    ```python
    default_export = next((e for e in non_shared_file.default_exports), None)
    if default_export:
        default_export.make_non_default()
    ```

    For each default export:

    1. Find the corresponding export in the non-shared file
    2. Convert it to a named export using `make_non_default()`
  </Accordion>

  <Accordion title="File Path Handling">
    ```python
    # Get corresponding non-shared file
    non_shared_path = target_file.filepath.replace('/shared/', '/')
    if not codebase.has_file(non_shared_path):
        print(f"⚠️ No matching non-shared file for: {filepath}")
        continue

    non_shared_file = codebase.get_file(non_shared_path)
    ```

    The code:

    1. Maps shared files to their non-shared counterparts
    2. Verifies the non-shared file exists
    3. Loads the non-shared file for processing
  </Accordion>
</AccordionGroup>

## Best Practices

1. **Check for Missing Files**: Always verify files exist before processing:

```python
if not target_file:
    print(f"⚠️ Target file not found: {filepath}")
    continue
```

2. **Log Progress**: Add logging to track the conversion process:

```python
print(f"📄 Processing {target_file.filepath}")
print(f"  🔄 Converting default export '{export.name}'")
```

3. **Handle Missing Exports**: Check that default exports exist before converting:

```python
default_export = next((e for e in non_shared_file.default_exports), None)
if default_export:
    default_export.make_non_default()
```

## Next Steps

After converting default exports:

1. Run your test suite to verify everything still works
2. Update any import statements that were using default imports
3. Review the changes to ensure all exports were converted correctly
4. Consider adding ESLint rules to prevent new default exports

<Note>
  Remember to test thoroughly after converting default exports, as this change affects how other files import the converted modules.
</Note>

### Related tutorials

* [Managing typescript exports](/tutorials/managing-typescript-exports)
* [Exports](/building-with-codegen/exports)
* [Dependencies and usages](/building-with-codegen/dependencies-and-usages)

## Complete Codemod

Here's the complete codemod that you can copy and use directly:

```python

for file in codebase.files:
    target_file = file.filepath
    if not target_file:
        print(f"⚠️ Target file not found: {filepath}")
        continue

    # Get corresponding non-shared file
    non_shared_path = target_file.filepath.replace('/shared/', '/')
    if not codebase.has_file(non_shared_path):
        print(f"⚠️ No matching non-shared file for: {filepath}")
        continue

    non_shared_file = codebase.get_file(non_shared_path)
    print(f"📄 Processing {target_file.filepath}")

    # Process individual exports
    for export in target_file.exports:
        # Handle default exports
        if export.is_reexport() and export.is_default_export():
            print(f"  🔄 Converting default export '{export.name}'")
            default_export = next((e for e in non_shared_file.default_exports), None)
            if default_export:
                default_export.make_non_default()

    print(f"✨ Fixed exports in {target_file.filepath}")

```
