How to Import SVGs into NextJS properly

How to Import SVGs into NextJS properly

Have you ever tried to import an SVG into your NextJS project, and ran into the following cryptic error?Five different ways to do it. How To Import SVGs into NextJS

Have you ever tried to import an SVG into your NextJS project, and ran into the following cryptic error?

Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type

In this error, webpack is telling us that it doesn’t have a loader set up to handle this particular file type (.svg files). Remember that with webpack, we need loaders to preprocess any files that aren’t JavaScript. These loaders help us to import CSS files, images, videos, and in our case, SVG files.

How to import SVGs into your NextJS application

1. Use SVGR

SVGR is a tool that allows us to import SVGs into your React applications as React components. If you have used Create-React-App in the past, you might be familiar with this, as it comes built-in.

To use SVGR with our project, we need to first install the dependency @svgr/webpack, and then modify our next.config.js file as follows. For more information about customizing your webpack configuration, check out the official documentation.

typescript webpack nextjs javascript svg

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Creating React and TypeScript apps with Webpack

In this post, we will cover how to use webpack to bundle a React and TypeScript app. We will also include how we can use webpack to provide an awesome experience while developing our app. Our setup will ensure type checking and linting occurs in the webpack process, which will help code quality. How to use Webpack to bundle a React and TypeScript app

NativeScript From Scratch Part 2: TypeScript and Webpack Configuration

In this NativeScript tutorial, we'll pick up where we left off in part 1 and incrementally add TypeScript to our JavaScript mobile app project. I'll also show you how to configure Webpack to support TypeScript.

Getting Started with React, TypeScript and Webpack

Here's my take on a starter project using React, TypeScript and Webpack.

Integrating TypeScript with Webpack

In this lesson, we are going to learn how to compile a TypeScript project using Webpack. This setup becomes necessary if you want to bundle TypeScript containing ES6 or the CommonJS module system imports and exports.

Webpack 5 Builds for AWS Lambda Functions with TypeScript

Today’s post will be a short overview that bridges these together: it shows how I bundled the TypeScript code from the Twitter post with node modules and prepare it for deployment. Webpack 5 Builds for AWS Lambda Functions with TypeScript