Using Unplugin Icons in Next.js: Auto-Import Any Icon
A step-by-step guide to installing and using Unplugin Icons in Next.js to auto-import SVG icons from any icon set, on demand.
A step-by-step guide to installing and using Unplugin Icons in Next.js to auto-import SVG icons from any icon set, on demand.
In this guide, you will learn how to use Unplugin Icons in a Next.js application. You will go through the process of setting up a new Next.js project, configuring Unplugin Icons, and using them to leverage a huge collection of icons in your application.
High Quality Starter Kits with built-in authentication flow (Auth.js), object uploads (AWS, Clouflare R2, Firebase Storage, Supabase Storage), integrated payments (Stripe, LemonSqueezy), email verification flow (Resend, Postmark, Sendgrid), and much more. Compatible with any database (Redis, Postgres, MongoDB, SQLite, Firestore).
Get all 3 kits$147$99One-time license · Lifetime updates
You’ll need the following:
Let’s get started by creating a new Next.js project. Open your terminal and run the following command:
npx create-next-app@latest my-appWhen prompted, choose:
Yes when prompted to use TypeScript.No when prompted to use ESLint.Yes when prompted to use Tailwind CSS.No when prompted to use src/ directory.Yes when prompted to use App Router.No when prompted to customize the default import alias (@/*).Once that is done, move into the project directory and start the app in development mode by executing the following command:
cd my-appnpm run devThe app should be running on localhost:3000. Now, let’s close the development server to install Unplugin Icons.
Execute the commands below to install the necessary packages for using Unplugin Icons in Next.js:
# unplugin-icons package for Webpack compilernpm i -D unplugin-icons
# @iconify/json package for the icons collectionnpm i -D @iconify/json
# @svgr/core and @svgr/plugin-jsx packages for React compatibilitynpm i -D @svgr/core @svgr/plugin-jsxThe command installs the following libraries:
unplugin-icons: A package to access thousands of icons as components on-demand universally.@iconify/json: A big collection of open source vector icons, all validated, cleaned up and converted to the same easy to use format.@svgr/core: A package to transform SVGs into React components.@svgr/plugin-jsx: A package to transform SVG into JSX.Further, perform the following additions in next.config.mjs to use the Unplugin Icons integration:
import unpluginIcons from 'unplugin-icons/webpack'
/** @type {import('next').NextConfig} */const nextConfig = { webpack(config) { config.plugins.push( unpluginIcons({ compiler: 'jsx', jsx: 'react', }), ) return config },}
export default nextConfigTo make sure that you can leverage the type definitions of the Unplugin Icons in Next.js, you would want to globally define such specific Icon imports as React component imports. To do so, update the tsconfig.json file with the following code:
{ "compilerOptions": { "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "bundler", "resolveJsonModule": true, "isolatedModules": true, "types": ["unplugin-icons/types/react"], // [!code ++] // [!code focus] "jsx": "preserve", "incremental": true, "paths": { "@/*": ["./*"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], "exclude": ["node_modules"]}The code above simply marks each import from ~icons to be considered as a React component in your Next.js application.
Finally, import the icon and use it in your Next.js index route with the following code:
import ClockIcon from '~icons/mdi/clock'
export default function Home() { return ( <ClockIcon /> );}The code above imports an icon as a React component in the index route.
You’re all done now!
To test the application, prepare a build and run the preview server using the command below:
npm run build && npm run startIn this guide, you learned how to use Unplugin Icons in a Next.js application.
Working in another framework? See how to auto-import icons with Unplugin Icons in SvelteKit or Unplugin Icons in Astro with React.
If you have any questions or comments, feel free to reach out to me on Twitter.
How Astro 7.2 incremental static builds cut a one-post rebuild from ~16.5s to ~10s, restoring 637 of 661 pages from cache with the right cacheKey setup.
We're excited to announce that MailKite email support is now available in all LaunchFast starter kits. Send transactional emails through MailKite with a single environment variable change.
Hydration is expensive. A practical look at Astro islands: the cheaper options to exhaust first (static HTML, build-time data, server islands, progressive enhancement), and the cases where hydration is useful.