swc-project/swc · error
{id:?} should not be wrapped with a function
Error message
{id:?} should not be wrapped with a function What it means
Internal invariant in swc_bundler: `wrap_esm` (which wraps a module's exports in an IIFE for computed-key / `export *` handling) was called for a ModuleId that has no registered wrapped-ESM identifier in the scope (`scope.wrapped_esm_id(id)` returned None). Modules are marked for wrapping earlier in graph analysis, so this signals the module graph and scope metadata are out of sync - a bundler bug or corrupted state, not a user configuration problem.
Source
Thrown at crates/swc_bundler/src/bundler/chunk/computed_key.rs:47
///
/// ```ts
/// const _mod = (function(){
/// const arr = [1, 2, 3];
/// return {
/// arr,
/// };
/// })();
/// ```
pub(super) fn wrap_esm(
&self,
ctx: &Ctx,
id: ModuleId,
module: Modules,
) -> Result<Modules, Error> {
let span = DUMMY_SP;
let module_var_name = match self.scope.wrapped_esm_id(id) {
Some(v) => v,
None => bail!("{id:?} should not be wrapped with a function"),
};
let is_async = module.iter().any(|m| contains_top_level_await(m.1));
let mut additional_items = Vec::new();
module.iter().for_each(|(module_id, item)| {
match item {
// Handle `export *`-s from dependency modules.
//
// See: https://github.com/denoland/deno/issues/9200
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
ref specifiers,
with: Some(with),
..
})) if is_injected(with) => {
for s in specifiers {
if let ExportSpecifier::Named(ExportNamedSpecifier {View on GitHub (pinned to 5176682b65)
Solutions
- Upgrade swc/swc_core to a release where the bundler's scope registration is fixed
- Reduce the bundle input: remove `export *` chains or computed export keys to dodge the wrapping path
- If reproducible on latest, file an swc issue with the entry graph - it is an internal invariant violation
Defensive patterns
Strategy: try-catch
Try / catch
// Rust - catch the invariant and surface a reportable error
match bundler.bundle(entries) {
Ok(v) => { /* ... */ }
Err(e) => {
let msg = format!("{e:#}");
if msg.contains("should not be wrapped with a function") {
// swc_bundler internal invariant: report module graph, try fewer `export *`
return Err(anyhow::anyhow!("swc_bundler invariant hit; simplify re-exports and update swc_core: {msg}"));
}
return Err(e.into());
}
} Prevention
- Pin swc_core versions validated against your entry graphs
- Minimize `export *` chains and computed export keys in bundled sources
- Keep a minimal reproducer bundle for regression testing bundler upgrades
When it happens
Trigger: Bundling via swc_bundler with ESM graphs that trigger wrapping (computed export keys, `export *` re-export chains, cycles) where scope registration for the module was skipped - typically a version-specific regression.
Common situations: Rare in practice; surfaces in deno-style bundling or tools embedding swc_bundler after upgrading swc_core versions.
Related errors
- Plan does not contain bundle kind for {:?}
- Modules reexported with `export * as foo from './foo'` shoul
- Decl in ExportDecl: {:?}
- internal error: entered unreachable code
- Non-computed member expression with property other than iden
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/5b1676ffec366e79.
Report an issue: GitHub.