Skip to content

SyntaxError: Export before declaration

Quick summary

You referenced a name in an export statement before it was declared — the module cannot resolve the binding.

Why this happens

  • Placing export { x } at the top of the file before declaring x

  • Circular import where the exported name is not yet initialized

  • Renaming a declaration but forgetting to update the export

Minimal example

✗ Broken code
export { greet };
function greet() { return 'hi'; } // declared after export
✓ Fixed code
function greet() { return 'hi'; }
export { greet }; // declared first

The export references greet before it is declared, so the binding is missing. Moving the declaration above the export resolves it.

How to diagnose

  • Read the SyntaxError naming the missing export binding

  • Check that the exported name is declared earlier in the file

  • Look for circular imports that delay initialization

How to fix

  • Move the export after the declaration, or use export default on the declaration

  • Use inline export: export const x = ...

  • Break circular imports by moving shared code to a third module

How to prevent

  • Use inline export (export const/function) to keep export and declaration together

  • Avoid circular module dependencies

Related Resources

Related Glossary

Related Lessons

← Back to language