Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~4 min readRust
#Rust#Cargo#Compiler Errors#Modules
A Rust module tree with an unresolved cyan crate slot, an amber Cargo manifest card below it

Introduction & Quick Summary

text
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.toml for this crate, even if it is present in another crate of the workspace.
  • The dependency is renamed. serde_json = { package = "serde_json", version = "1" } makes serde_json unavailable; only the key is importable.
  • A local module is not declared. A file src/api/handler.rs is not compiled unless api/mod.rs or api.rs contains mod handler;.
  • The path form is wrong for the edition. In 2018 and later, a local item needs crate::, self:: or super::; in 2015 it needs extern crate.
  • A #[cfg] gate removed the module. The mod line exists behind a feature flag that is not enabled, so the name is not in scope in this build.
Path formResolves toEdition
serde_json::ValueExternal crate2018+
crate::api::handlerRoot of the current crate2018+
self::helperCurrent module2018+
super::helperParent module2018+
::serde_json::ValueExternal crate, explicit2018+
extern crate serde_json;Required declaration2015

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:

bash
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:

toml
[dependencies] serde_json = "1"

For the local-module variant, the layout matters:

text
src/main.rs src/api.rs <- declares mod handler; src/api/handler.rs <- defines the item
rust
// 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

SymptomUnderlying stateFix
undeclared crate for a known libraryNot in [dependencies]Add it
Same error only in one workspace memberWorkspace dep not inheriteddep = { workspace = true }
undeclared module for a local fileNo mod declarationAdd mod name;
Error only with --no-default-features#[cfg(feature)] gatingEnable the feature
Error inside a macro expansionMacro hygiene pathUse $crate:: in the macro
Error after use x as y;Alias, not original nameUse the alias
Editionextern crateLocal path prefixdyn Trait
2015RequiredBare nameOptional
2018Optionalcrate:: required in most casesRequired
2021Optionalcrate::, closure capture changesRequired
2024OptionalSame, with stricter unsafe and edition styleRequired

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:

toml
# 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

toml
[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:

toml
[dependencies] json = { package = "serde_json", version = "1" }
rust
use json::Value; // correct use serde_json::Value; // E0433

Case 3: mod inside a function

rust
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

rust
#[cfg(feature = "server")] pub mod server;
rust
#[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:

bash
cargo tree -e features -i my_crate | head -20

Case 5: Macro hygiene and $crate

rust
#[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

rust
// src/main.rs use crate::config::Settings; // fine in a binary

An 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 on extern crate, and pasting 2015 code into a 2021 crate produces E0433 for every external crate. Check the actual edition with cargo metadata.
  • cargo add in the wrong directory. In a workspace it updates the member's manifest, but a stray cargo add at the root without -p can 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 enabling dep:) 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.rs cannot 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 .rlib in place; cargo clean -p the-crate removes 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 documentation

The Rust documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Rust work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Rust guides

More in Rust

All Rust guides

The guides filed next to this one in Rust, in library order.