Using Unplugin Icons in Next.js
How to use Unplugin Icons in Next.js application?
How to use Unplugin Icons in Next.js application?
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.
If you have any questions or comments, feel free to reach out to me on Twitter.
Learn how to deploy a server-side rendered Angular 20+ application to Cloudflare Workers, serving static assets from the edge and running SSR.
LaunchFast works with Backblaze B2 out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at your regional B2 S3 endpoint and add your application key.
LaunchFast works with Tigris out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at https://fly.storage.tigris.dev and add your credentials.