Rust E0433: failed to resolve, undeclared crate or module
Name resolution failed before type checking. Check the manifest, the mod tree, edition path rules and feature gates before rewriting the path.

Introduction & Quick Summary
error[E0433]: failed to resolve: use of undeclared crate or module `serde_json`
--> src/api/handler.rs:12:20
|
12 | let body = serde_json::json!({ "ok": true });
| ^^^^^^^^^^ use of undeclared crate or module `serde_json`The path in your code starts with a name the compiler cannot resolve in the current scope. E0433 is a name resolution failure, so it is reported before type checking and often before the borrow checker has anything to say: the compiler never got that far.
The five causes worth checking first:
- The dependency is not in
Cargo.tomlfor this crate, even if it is present in another crate of the workspace. - The dependency is renamed.
serde_json = { package = "serde_json", version = "1" }makesserde_jsonunavailable; only the key is importable. - A local module is not declared. A file
src/api/handler.rsis not compiled unlessapi/mod.rsorapi.rscontainsmod handler;. - The path form is wrong for the edition. In 2018 and later, a local item needs
crate::,self::orsuper::; in 2015 it needsextern crate. - A
#[cfg]gate removed the module. Themodline exists behind a feature flag that is not enabled, so the name is not in scope in this build.
| Path form | Resolves to | Edition |
|---|---|---|
serde_json::Value | External crate | 2018+ |
crate::api::handler | Root of the current crate | 2018+ |
self::helper | Current module | 2018+ |
super::helper | Parent module | 2018+ |
::serde_json::Value | External crate, explicit | 2018+ |
extern crate serde_json; | Required declaration | 2015 |
Core Mechanics, Signatures & Reproducible Setup
Rust 2018 changed path resolution so that use paths and expression paths both start from the crate root or an external crate name, and crate:: became the explicit way to reach local items. Cargo passes --extern name=path for each dependency, which is what makes serde_json:: resolve at all; without the dependency there is no such flag and the name does not exist.
A minimal reproduction is one missing line:
cargo new --bin e0433-demo && cd e0433-demo
cat > Cargo.toml <<'TOML'
[package]
name = "e0433-demo"
version = "0.1.0"
edition = "2021"
TOML
cat > src/main.rs <<'RS'
fn main() {
let value = serde_json::json!({"ok": true});
println!("{value}");
}
RS
cargo build
# error[E0433]: failed to resolve: use of undeclared crate or module `serde_json`The fix is to declare the dependency, not to write a longer path:
[dependencies]
serde_json = "1"For the local-module variant, the layout matters:
src/main.rs
src/api.rs <- declares mod handler;
src/api/handler.rs <- defines the item// src/api.rs
pub mod handler;Without pub mod handler;, handler.rs is never compiled and every reference to crate::api::handler::… fails with E0433. A common symptom is an "unused file" that the compiler never complains about, because it is not part of the module tree at all.
Key Comparison Table
| Symptom | Underlying state | Fix |
|---|---|---|
undeclared crate for a known library | Not in [dependencies] | Add it |
| Same error only in one workspace member | Workspace dep not inherited | dep = { workspace = true } |
undeclared module for a local file | No mod declaration | Add mod name; |
Error only with --no-default-features | #[cfg(feature)] gating | Enable the feature |
| Error inside a macro expansion | Macro hygiene path | Use $crate:: in the macro |
Error after use x as y; | Alias, not original name | Use the alias |
| Edition | extern crate | Local path prefix | dyn Trait |
|---|---|---|---|
| 2015 | Required | Bare name | Optional |
| 2018 | Optional | crate:: required in most cases | Required |
| 2021 | Optional | crate::, closure capture changes | Required |
| 2024 | Optional | Same, with stricter unsafe and edition style | Required |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: Workspace inheritance without the root declaration
A member crate with serde_json = { workspace = true } compiles only if the workspace root declares it:
# workspace Cargo.toml
[workspace]
members = ["api", "core"]
[workspace.dependencies]
serde_json = "1"The failure mode is subtle: Cargo accepts the member file and the error appears at compile time, not at manifest load.
Case 2: Dependency renamed in the manifest
[dependencies]
serde_json = { package = "serde_json", version = "1" }This entry is importable as serde_json because the key is the same. Renaming the key changes the import name:
[dependencies]
json = { package = "serde_json", version = "1" }use json::Value; // correct
use serde_json::Value; // E0433Case 3: mod inside a function
fn run() {
mod inner; // looks for src/inner.rs, not a nested file
}Inline modules in a function cannot be used as a shortcut for a file path relative to the caller. Declare modules at the crate or parent module level and reference them with crate::.
Case 4: Feature-gated modules
#[cfg(feature = "server")]
pub mod server;#[cfg(feature = "server")]
use crate::server::Router;Without features = ["server"], the module is absent and any unconditional reference fails. Check the resolved features rather than the manifest:
cargo tree -e features -i my_crate | head -20Case 5: Macro hygiene and $crate
#[macro_export]
macro_rules! make_client {
() => { Client::new() }; // resolves at the call site
}Inside an exported macro, use $crate::Client::new() so the path resolves in the defining crate instead of the caller's scope, which is the classic cause of E0433 that only appears in downstream crates.
Case 6: crate:: pointing at a binary-only crate
// src/main.rs
use crate::config::Settings; // fine in a binaryAn integration test under tests/ is a separate crate, so crate:: refers to the test crate, not the library. Move shared code into src/lib.rs and import it by name, or use crate_name:: and add the package as a dev-dependency on itself.
Common Compilation/Runtime Mistakes & Version Gotchas
edition = "2015"in a copied manifest. Old examples rely onextern crate, and pasting 2015 code into a 2021 crate produces E0433 for every external crate. Check the actual edition withcargo metadata.cargo addin the wrong directory. In a workspace it updates the member's manifest, but a straycargo addat the root without-pcan add the dependency where it is unused.- Optional dependencies.
dep = { version = "1", optional = true }is not available unless the feature named after the dependency (or a feature enablingdep:) is active. #[path]attributes.#[path = "../generated/mod.rs"] mod generated;changes resolution for that module and can silently break nested paths, producing E0433 further down.- Build scripts. A
build.rscannot use normal dependencies unless they are in[build-dependencies], and the resulting error looks identical. - Stale
target/after a rename. Renaming a dependency key can leave the old.rlibin place;cargo clean -p the-crateremoves the confusion faster than reasoning about it.
When the manifest is the suspect, compare the working and failing Cargo.toml with the diff tool, and inspect the resolved graph with cargo metadata --format-version 1 piped into the JSON formatter to see the exact dependency names and features Cargo chose. If the failing path appears only in CI, confirm the feature set in the environment with the env file viewer before changing code.
Primary references: the E0433 error index entry lists the resolution cases, and Cargo dependency specification covers renaming, optional dependencies and workspace inheritance.
Official references
primary documentationThe Rust documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Rust work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Rust guides
All Rust guidesRust E0061: this function takes 1 argument but 2 were supplied
E0061 counts arguments at the call site. Check the signature, method versus associated function, generic defaults, and trait impls that changed arity.
Rust E0282: type annotations needed
E0282 means inference ran out of information. Annotate the binding, add a turbofish to collect or parse, or fix the signature the value flows into.
Rust E0463: can't find crate for std
The target triple has no standard library installed. Add the target with rustup, check the target string and link the right ABI.
Rust E0432: unresolved import
E0432 is a module graph problem: a missing mod declaration, a private parent or a wrong path. Fix the tree, not the dependency list.
More in Rust
All Rust guidesThe guides filed next to this one in Rust, in library order.