denoland/deno · error
Unexpected media type {media_type} for {specifier}
Error message
Unexpected media type {media_type} for {specifier} What it means
The graph-based module loader in libs/resolver hands sources to the runtime only for media types it can emit as JavaScript. When a module resolves to Css, Html, Jsonc, Json5, Markdown, Sql, Wasm, or SourceMap on this path, it panics because no JS emit exists for that type.
Source
Thrown at libs/resolver/loader/module_loader.rs:627
MediaType::TypeScript
| MediaType::Mts
| MediaType::Jsx
| MediaType::Tsx => {
return Ok(Some(CodeOrDeferredEmit::DeferredEmit {
specifier,
media_type: *media_type,
source: &source.text,
}));
}
MediaType::Css
| MediaType::Html
| MediaType::Jsonc
| MediaType::Json5
| MediaType::Markdown
| MediaType::Sql
| MediaType::Wasm
| MediaType::SourceMap => {
panic!("Unexpected media type {media_type} for {specifier}")
}
};
// at this point, we no longer need the parsed source in memory, so free it
self.parsed_source_cache.free(specifier);
Ok(Some(CodeOrDeferredEmit::Source(LoadedModule {
source: LoadedModuleSource::ArcStr(code),
specifier: Cow::Borrowed(specifier),
media_type: *media_type,
})))
}
},
Some(deno_graph::Module::Wasm(WasmModule {
source, specifier, ..
})) => Ok(Some(CodeOrDeferredEmit::Source(LoadedModule {
source: LoadedModuleSource::ArcBytes(source.clone()),
specifier: Cow::Borrowed(specifier),View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Load the asset with a read API instead of an ESM import
- Import a supported module type (JS/TS/JSON as allowed by your runtime configuration)
- Configure the transpile or asset pipeline for that media type before this loader path runs
- Check the resolved MediaType up front and reject non-JS/TS types with a real error
Example fix
// before
import styles from "./styles.css"; // panics: Unexpected media type Css
// after
const styles = await Deno.readTextFile(new URL("./styles.css", import.meta.url)); Defensive patterns
Strategy: validation
Validate before calling
use deno_ast::MediaType;
fn unusable_as_module_source(mt: MediaType) -> bool {
matches!(
mt,
MediaType::Css | MediaType::Html | MediaType::Jsonc | MediaType::Json5
| MediaType::Markdown | MediaType::Sql | MediaType::Wasm | MediaType::SourceMap
)
}
// reject before the loader panics
if unusable_as_module_source(media_type) {
return Err(format!("{specifier} cannot be loaded as a module source"));
} Prevention
- Keep ESM imports limited to JS/TS-family files; load assets with read APIs
- In embedders, resolve the MediaType and reject unsupported types with a real error before handing off to the loader
- Lint the codebase for imports of .css/.md/.sql/.html extensions in CI
When it happens
Trigger: Importing a .css, .html, .jsonc, .json5, .md, .sql, or .wasm file through a loader configuration that expects JS source output, without the asset/transpile pipeline the Deno CLI normally runs.
Common situations: Porting web code that imports stylesheets or markdown; importing JSON5/JSONC configs as modules; embedders reusing the resolver loader without configuring transpilation.
Related errors
- Unsupported media type for snapshotting {media_type:?} for f
- pledge test permissions called before restoring previous ple
- restore test permissions token does not match the stored tok
- pledge test permissions called before restoring previous ple
- restore test permissions token does not match the stored tok
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/4bb195c2dfee505f.
Report an issue: GitHub.