swc-project/swc · error
failed parse json as javascript object
Error message
failed parse json as javascript object: {err:#?} What it means
The bundler's JSON loader parses a .json module's contents as a single JS expression via parse_file_as_expr and wraps any parse failure with this error. It means the file being loaded as a JSON module is not a valid JSON/JS expression; the attached err debug carries the line/column.
Solutions
- Fix the JSON file so it parses as an expression (see line/column in the wrapped error)
- Confirm the import resolves to a real JSON file, not JSON5/JSONC that the default Syntax rejects
- Handle the returned Error in load_with_handler and fall back to another loader
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at crates/swc_node_bundler/src/loaders/json.rs:17 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/d084dac273952475.
Report an issue: GitHub.
Appendix: source
Thrown at crates/swc_node_bundler/src/loaders/json.rs:17
use std::sync::Arc;
use anyhow::{anyhow, Error};
use swc_atoms::atom;
use swc_common::{SourceFile, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_parser::{parse_file_as_expr, Syntax};
pub(super) fn load_json_as_module(fm: &Arc<SourceFile>) -> Result<Module, Error> {
let expr = parse_file_as_expr(
fm,
Syntax::default(),
EsVersion::Es2020,
None,
&mut Vec::new(),
)
.map_err(|err| anyhow!("failed parse json as javascript object: {err:#?}"))?;
let export = ExprStmt {
span: DUMMY_SP,
expr: AssignExpr {
span: DUMMY_SP,
op: op!("="),
left: MemberExpr {
span: DUMMY_SP,
obj: Box::new(Ident::new_no_ctxt(atom!("module"), DUMMY_SP).into()),
prop: MemberProp::Ident(IdentName::new(atom!("exports"), DUMMY_SP)),
}
.into(),
right: expr,
}
.into(),
}
.into();
View on GitHub (pinned to 5176682b65)