This article is about fixing import ReactComponent from svg file is not working in facebook create-react-app
  • 26-Feb-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing import ReactComponent from svg file is not working in facebook create-react-app

import ReactComponent from svg file is not working in facebook create-react-app

Lightrun Team
Lightrun Team
26-Feb-2023

Explanation of the problem

The user is experiencing an error while importing an SVG file into a TypeScript application. The error message indicates that the property ‘children’ of the file is undefined. The user is attempting to import the file using the following code: import {ReactComponent as SomeName} from ‘./some/svg/file.svg’. The user has tried reinstalling all dependencies, but the error persists. The user has searched the user guide for information on SVG and TypeScript.

Environment:

The user is running create-react-app version 4.0.1. An attempt to retrieve additional environment information using envinfo failed due to an error in the wmic os get Caption command.

Steps to reproduce:

The user created a new project, moved all files to the new project, and attempted to import the SVG file again. The issue persisted.

Expected behavior:

The SVG files should be imported as React components.

Actual behavior:

The SVG file import is not working.

Reproducible demo:

The user was unable to provide a demo but shared the contents of their package.json file.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for import ReactComponent from svg file is not working in facebook create-react-app

If you’re trying to import an SVG file into a React component using import { ReactComponent } from 'path/to/file.svg', but it’s not working in a Create React App project, there are a few things you can try:

  1. Make sure you have the necessary dependencies installed: Create React App uses Babel to transpile the code and the babel-plugin-inline-react-svg plugin to handle importing SVGs. Ensure you have installed it by running the following command:
npm install --save-dev babel-plugin-inline-react-svg
  1. Configure Babel to use the babel-plugin-inline-react-svg plugin: To configure Babel, create a file named .babelrc in the root of your project, and add the following code:
{
  "plugins": [
    ["inline-react-svg", {
      "svgo": {
        "plugins": [
          { "removeViewBox": false }
        ]
      }
    }]
  ]
}
  1. Ensure that your SVG file is valid: Sometimes, the problem might be with the SVG file itself. Ensure that your SVG is valid and doesn’t contain any syntax errors. You can use online SVG validators like https://validator.w3.org/ or https://jakearchibald.github.io/svgomg/ to validate your SVG file.
  2. Use an alternate syntax to import the SVG file: In some cases, the import { ReactComponent } from 'path/to/file.svg' syntax might not work. You can try using an alternate syntax to import the SVG file as follows:
import React from 'react';
import { ReactComponent as MySvg } from './path/to/file.svg';

function MyComponent() {
  return (
    <div>
      <MySvg />
    </div>
  );
}

export default MyComponent;

 

Other popular problems with facebook create-react-app

Problem: “Module not found” error

One of the most common problems with Facebook’s Create-React-App (CRA) is the “Module not found” error. This error occurs when the app is not able to locate a module or file that it needs to run. It can happen due to a variety of reasons, including incorrect file paths, missing dependencies, or outdated package versions. The error message usually contains the name of the missing module or file and the file path where it was expected to be found.

Solution:

To fix this error, you can start by double-checking the file paths and ensuring that all the required dependencies are installed and up-to-date. You can also try clearing the cache and restarting the server. If the problem persists, you may need to reinstall the dependencies or update your package versions. You can use the “npm install” or “yarn install” command to do this.

Problem: “Cannot read property” error

Another common problem with CRA is the “Cannot read property” error. This error occurs when the app is trying to access a property or method that does not exist or is undefined. It can happen due to typos, incorrect syntax, or unexpected changes in the data structure. The error message usually contains the name of the property or method that is causing the problem and the file path where it is located.

Solution:

To fix this error, you can start by checking the syntax and data structure to ensure that the property or method exists and is defined correctly. You can also use console.log() or debugger to track the flow of data and identify where the problem is occurring. Additionally, you can use the “typeof” operator to check the data type of the property or method before accessing it.

Problem: “Maximum call stack size exceeded” error

A third common problem with CRA is the “Maximum call stack size exceeded” error. This error occurs when the app enters into an infinite loop or recursive function that keeps calling itself without exiting. It can happen due to logic errors, incorrect conditions, or unexpected data inputs. The error message usually contains the name of the function or file where the infinite loop is occurring.

Solution:

To fix this error, you can start by reviewing the logic and conditions of the function to ensure that it is not entering into an infinite loop. You can also use console.log() or debugger to track the flow of data and identify where the function is getting stuck. Additionally, you can set a breakpoint or a limit to the number of iterations in the function to prevent it from entering into an infinite loop.

A brief introduction to facebook create-react-app

Facebook’s create-react-app is a tool that simplifies the process of creating and deploying React applications. It provides developers with a streamlined way to start building React applications without having to configure the build tools themselves. The create-react-app tool provides a pre-configured environment that includes a set of tools and dependencies that are commonly used when building React applications.

Create-react-app generates a basic project structure with a pre-configured webpack and babel setup, which allows developers to use the latest features of JavaScript, including JSX syntax. The tool also comes with a development server that allows for live reloading of the application when changes are made. Additionally, create-react-app includes a set of scripts that enable developers to build and deploy the application to different environments. The tool provides a simple, yet powerful, way to create and manage React applications, making it a popular choice among developers.

Most popular use cases for facebook create-react-app

  1. Rapid prototyping of React applications: Facebook’s Create React App is an ideal choice for developers looking to rapidly prototype React applications. The tool takes care of many of the configuration details that can often cause headaches for developers, allowing them to focus on writing code and building out features. With Create React App, developers can quickly spin up new projects, add dependencies, and start building out their components and views.
  2. Code sharing and portability: Create React App enables code sharing and portability across different environments, making it easier for teams to collaborate and maintain their codebase. By using standardized tooling and build processes, Create React App ensures that developers can easily share and deploy their code to various platforms and environments. This can be particularly useful for teams that work across multiple projects or platforms, as it helps to reduce the overhead of managing different build configurations.
  3. Modern web development: With its support for modern web development practices, Create React App allows developers to build applications using the latest technologies and techniques. The tool comes with built-in support for features like hot reloading, which allows developers to see changes to their code in real-time without having to refresh their browser. Create React App also supports code splitting, which can help improve the performance of large applications by breaking them down into smaller, more manageable chunks. Overall, Create React App is a powerful tool for modern web development that can help developers build high-quality, performant React applications.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.