swc-project/swc · error

The requested const_module `{:?}` does not provide default e

Error message

The requested const_module `{:?}` does not provide default export

What it means

For `import DefaultName from 'mod'` where 'mod' is a const module, the transform looks up the literal key "default" in that module's globals map. If no "default" entry exists it panics with 'The requested const_module `{src}` does not provide default export'.

Source

Thrown at crates/swc_ecma_transforms_optimization/src/const_modules.rs:133

                                    .unwrap_or_else(|| s.local.sym.clone().into());
                                let value = entry.get(&imported).cloned().unwrap_or_else(|| {
                                    panic!(
                                        "The requested const_module `{:?}` does not provide an \
                                         export named `{:?}`",
                                        import.src.value, imported
                                    )
                                });
                                self.scope.imported.insert(imported.clone(), value);
                            }
                            ImportSpecifier::Namespace(ref s) => {
                                self.scope.namespace.insert(s.local.to_id());
                            }
                            ImportSpecifier::Default(ref s) => {
                                let imported: Wtf8Atom = s.local.sym.clone().into();
                                let default_import_key: Wtf8Atom = atom!("default").into();
                                let value =
                                    entry.get(&default_import_key).cloned().unwrap_or_else(|| {
                                        panic!(
                                            "The requested const_module `{:?}` does not provide \
                                             default export",
                                            import.src.value
                                        )
                                    });
                                self.scope.imported.insert(imported, value);
                            }
                            #[cfg(swc_ast_unknown)]
                            _ => panic!("unable to access unknown nodes"),
                        };
                    }

                    None
                } else {
                    Some(import.into())
                }
            }
            _ => Some(item),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add a "default" entry to the module's globals map with the replacement expression
  2. Switch the code to a named import that already has a globals entry
  3. Remove the default import if unintended

Example fix

// before
import env from '@app/env';
// globals: { "@app/env": { "MODE": "'dev'" } }

// after
// globals: { "@app/env": { "default": "'dev'", "MODE": "'dev'" } }
import env from '@app/env';
Defensive patterns

Strategy: validation

Validate before calling

// JS: every module receiving a default import needs a "default" entry
function checkDefaultImports(src, globals) {
  const re = /import\s+([\w$]+)\s+from\s+['"]([^'"]+)['"]/g;
  for (const [, , mod] of src.matchAll(re)) {
    if (globals[mod] && !('default' in globals[mod]))
      throw new Error(`const module "${mod}" needs a "default" entry for default imports`);
  }
}

Prevention

When it happens

Trigger: Default-importing a const module (import cfg from 'config') whose globals map defines only named keys and no "default" key.

Common situations: Adding a default import to code while the globals config was written for named imports only; assuming the first entry or the module name acts as the default value.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/fc7d0e5d7cffdc66. Report an issue: GitHub.