Home › Rust

error[E0433]: failed to resolve: use of undeclared crate or module (Rust) — how to fix

error[E0433]: failed to resolve: use of undeclared crate or module `serde_json`
 --> src/main.rs:4:16
  |
4 |     let v = serde_json::from_str(&data)?;
  |             ^^^^^^^^^^ use of undeclared crate or module `serde_json`

Quick fix: For external crates add them: cargo add serde_json (note: hyphens in crate names become underscores in code). For your own modules declare them with mod utils; in main.rs/lib.rs and reference them as crate::utils::.... For std types, import them: use std::collections::HashMap;.

What it means

In a path like a::b::c, rustc resolves a first. E0433 means a is not a known crate, module or type in this scope. Related messages:

Cause 1: crate not added to Cargo.toml

Extern crates only exist if they're dependencies:

cargo add serde_json
cargo add tokio --features full

or by hand:

[dependencies]
serde_json = "1"

If it's only in [dev-dependencies], it's available in tests and examples, not in src/. In a workspace, add it to the member crate's Cargo.toml, not just the root.

Cause 2: hyphen vs underscore

Crate names with hyphens are referenced with underscores in code:

[dependencies]
rand-core = "0.6"
use rand_core::RngCore;     // not rand-core

A few packages also set a different library name via [lib] name; the crate's docs.rs page shows the name to use in code. You can also rename a dependency yourself: foo = { package = "real-name", version = "1" }.

Cause 3: your own module isn't declared

Creating src/utils.rs is not enough. Declare it in the crate root (main.rs or lib.rs):

mod utils;

fn main() {
    utils::helper();
}

From another module, refer to it from the crate root:

use crate::utils::helper;      // absolute
use super::utils::helper;      // relative to parent module

In a binary + library package, code in main.rs must use the library crate's name: use my_package::utils; (with hyphens converted to underscores), not crate::utils.

Cause 4: std types used without import

let mut m = HashMap::new();    // E0433: use of undeclared type `HashMap`

Only a few items are in the prelude. Import the rest:

use std::collections::HashMap;
use std::fs;
use std::sync::Arc;

Cause 5: feature-gated items

Many crates hide modules behind features. tokio::fs, tokio::net and #[tokio::main] need features (full, fs, macros, rt-multi-thread); serde::Deserialize derive needs features = ["derive"]. Check the crate's docs.rs page — gated items are labelled "Available on crate feature X only".

cargo add tokio --features macros,rt-multi-thread

Cause 6: edition 2015 paths

In edition 2015, paths in use were relative to the crate root and extern crates needed extern crate foo;. Make sure Cargo.toml has edition = "2021" (or "2024") so modern path rules apply.

Cause 7: wrong module visibility

If the module exists but isn't pub, you'll usually get E0603 ("module is private") instead. Make it pub mod utils; when using it from another crate.

After fixing, cargo check is the fastest way to confirm.

Last updated 2026-11-09.