Issue with Selective Icon Import from react-icons
in Next.js
#80799
-
SummaryI followed the instructions from the official Next.js documentation How to optimize your local development environment#3-check-your-imports to avoid importing thousands of icons at once. However, the suggested method doesn’t seem to work, and I encountered the following error: // Before
import { MdAttachment, MdOutlineDevices } from "react-icons/md"; // After
import MdAttachment from "react-icons/md/MdAttachment";
import MdOutlineDevices from "react-icons/md/MdOutlineDevices"; Error Messages: Cannot find module 'react-icons/md/MdAttachment' or its corresponding type declarations.ts(2307)
Cannot find module 'react-icons/md/MdOutlineDevices' or its corresponding type declarations.ts(2307) It’s clear that the documented method is not applicable here. What’s the correct way to import only the icons I need from Additional informationI found a similar import pattern in the v2 documentation for // OLD IMPORT STYLE
import FaBeer from "react-icons/lib/fa/beer";
function Question() {
return (
<h3>
Lets go for a <FaBeer />?
</h3>
);
} Additionally, the README also mentions another import method using the import { FaBeer } from "@react-icons/all-files/fa/FaBeer";
function Question() {
return (
<h3>
Lets go for a <FaBeer />?
</h3>
);
} So, what’s the correct and recommended way to import only the icons I need in Next.js (App Router) without pulling in the entire icon library? ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @1chooo 👋, You're right, the method like: import MdAttachment from "react-icons/md/MdAttachment"; doesn't work because react-icons doesn't expose icons like that as separate modules in the default package. To import only the icons you need without loading the full bundle in Next.js, follow this: Instead of the default react-icons package, use: npm install @react-icons/all-files Then import icons like this: import { MdAttachment } from "@react-icons/all-files/md/MdAttachment"; This works with Next.js (including App Router) and helps optimize your bundle size by importing only what you use. Why Not Default react-icons? Hope this clears it up! |
Beta Was this translation helpful? Give feedback.
Hi @1chooo 👋,
You're right, the method like:
import MdAttachment from "react-icons/md/MdAttachment"; doesn't work because react-icons doesn't expose icons like that as separate modules in the default package.
To import only the icons you need without loading the full bundle in Next.js, follow this:
Instead of the default react-icons package, use:
npm install @react-icons/all-files
Then import icons like this:
import { MdAttachment } from "@react-icons/all-files/md/MdAttachment";
import { MdOutlineDevices } from "@react-icons/all-files/md/MdOutlineDevices";
This works with Next.js (including App Router) and helps optimize your bundle size by importing only what you use.
Why Not Default rea…