create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

SVGR generates invalid react component

Open fkrauthan opened this issue 3 years ago • 25 comments

Describe the bug

I've just upgraded to react-scripts 5.0 but my simple import of a Logo svg fails with the error:

ERROR in ./src/components/layout/logo.svg
Module build failed (from ./node_modules/@svgr/webpack/lib/index.js):
SyntaxError: unknown: Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can set `throwIfNamespace: false` to bypass this warning.

I wasn't even planning to use the module and instead just use an img tag with the SVG as source (similar to what the sample application does).

Did you try recovering your dependencies?

This does not seem to be a dependency issue.

Which terms did you search for in User Guide?

I've tried to search for SVG, files as well as creating a new getting started to compare the projects. The only thing I can see is that the sample SVG is a lot simpler then the Logo I am trying to use.

Environment

Environment Info:

  current version of create-react-app: 5.0.0
  running from C:\Users\fkrauthan\AppData\Local\Yarn\Data\global\node_modules\create-react-app

  System:
    OS: Windows 10 10.0.19043
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
  Binaries:
    Node: 17.2.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.1.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 96.0.4664.93
    Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.53)
    Internet Explorer: 11.0.19041.1202
  npmPackages:
    react: ^17.0.2 => 17.0.2
    react-dom: ^17.0.2 => 17.0.2
    react-scripts: 5.0.0 => 5.0.0
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Create a new react application
  2. Replace the logo.svg with the content of a complex file (e.g. masthead.svg)
  3. Try to run yarn run start or npm run start

Expected behavior

Ether a way that I can disable the component generation (in case I don't need it) or a valid component that actually compiles with the react-scripts provided build setup/

Actual behavior

Code is not compiling as soon as I import the SVG with the error:

ERROR in ./src/components/layout/logo.svg
Module build failed (from ./node_modules/@svgr/webpack/lib/index.js):
SyntaxError: unknown: Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can set `throwIfNamespace: false` to bypass this warning.

Reproducible demo

The setup to reproduce is pretty simple. See above for instructions. But here is a project as well test_app.zip

fkrauthan avatar Dec 14 '21 21:12 fkrauthan

I got same issue.

I used xmlnsXlink to replace xmlns:xlink, which works on some svg files. My final solution is that I minify svg files. You can try it on https://www.svgminify.com/ .

damozhang avatar Dec 15 '21 00:12 damozhang

@damozhang I've tried using that but seems like that SVG file in particular still contains namespaces that trigger that error even after using that website to minify. And in general I shouldn't have todo that in first place. I feel like there is some configuration wrong/missing.

fkrauthan avatar Dec 15 '21 00:12 fkrauthan

same here, I import a svg and use it as a source to img, started getting this error after trying to upgrade to cra 5.

wmitsuda avatar Dec 15 '21 14:12 wmitsuda

Exactly same issue here

DaviGn avatar Dec 17 '21 16:12 DaviGn

Same here.

dansenpir avatar Dec 24 '21 19:12 dansenpir

Same

Tonacatecuhtli avatar Jan 12 '22 07:01 Tonacatecuhtli

Same here

graemeveygo avatar Jan 13 '22 14:01 graemeveygo

I imagine this probably ultimately caused by the problem with regular import described in #11770 rather than SVGR specifically.

matburnham avatar Jan 15 '22 23:01 matburnham

I find a similar issue on stackoverflow I didn't minify my svg file but in my case, i changed xmlns:bx to xmlnsBx and bx:shape to bxShape into my svg code file. Now, it works for me

belayaShlyapa avatar Feb 14 '22 14:02 belayaShlyapa

same here, I would rather see a different fix than modifying all the svg-s after they get updated again ang again.

stenzing avatar Feb 27 '22 13:02 stenzing

Has anyone found a fix/workaround for this issue yet?

azizur avatar Apr 06 '22 16:04 azizur

Has anyone found a fix/workaround for this issue yet?

it would be good to have some feedback from the team, it's been more than 3 months and no idea if there is some working on it, it still has the "needs triage" tag.

wmitsuda avatar Apr 06 '22 17:04 wmitsuda

Looks like there are a few open PR that could resolve this issue https://github.com/facebook/create-react-app/issues/12146, https://github.com/facebook/create-react-app/pull/12026, https://github.com/facebook/create-react-app/pull/11780, https://github.com/facebook/create-react-app/pull/12172

azizur avatar Apr 07 '22 03:04 azizur

@fkrauthan and everyone else here, i too got this error but then found my SVG file was corrupted after setting up eslint, found there was one semicolon that got added in SVG file content. After removing it, the error got resolved.

abinanto avatar Apr 27 '22 14:04 abinanto

My file is definitely not corrupted as it works fine in inkscape and online tools. Plus the older version can without any issues display it.

fkrauthan avatar Apr 27 '22 14:04 fkrauthan

Hi everybody. I have same problem . after search and debug i use this method and my problem solved : I convert my svg file to png and reconvert from png to svg and upgrade react-scripts to 5.0.1 ( react project )

ekhtiari avatar Apr 29 '22 07:04 ekhtiari

I got same issue.

I used xmlnsXlink to replace xmlns:xlink, which works on some svg files. My final solution is that I minify svg files. You can try it on https://www.svgminify.com/ .

This worked for me thankyou for providing neccessary resource links.

SiddharthAgarwal25 avatar May 06 '22 11:05 SiddharthAgarwal25

use xmlnsSvg instead of xmlns:svg in the svg file.

elvinhatamov avatar Aug 09 '22 02:08 elvinhatamov

Converting your jpg or png file to SVG would solve this problem.

oyebanjiyusuf3 avatar Nov 20 '22 03:11 oyebanjiyusuf3

@fkrauthan and everyone else here, i too got this error but then found my SVG file was corrupted after setting up eslint, found there was one semicolon that got added in SVG file content. After removing it, the error got resolved.

thanks. this helped me

dayoolacodes avatar Dec 14 '22 09:12 dayoolacodes

Converting your jpg or png file to SVG would solve this problem.

Yeah but kinda defeats the purpose of using SVG in first place.

I also don't think its some illegal character as SVG programs can show it. And re-exporting through Inkscape yielded the same result.

fkrauthan avatar Dec 14 '22 19:12 fkrauthan

Same with me here, I upgraded react scripts from 4.0.3 to 5.0.0 and am getting the same error. Please let me know if any workarounds

GourishettySairam avatar Sep 06 '23 06:09 GourishettySairam

still no solution?

lennybeadle avatar Dec 10 '23 00:12 lennybeadle

Same with me here, I upgraded react scripts from 4.0.3 to 5.0.0 and am getting the same error. Please let me know if any workarounds

My working solution: Previous I was using some svg's that were taken from AdobeXD, I noticed that I was getting this error only for these type of SVG's. So I manually converted those SVG's using figmaXD to other SVG's type. The error was gone. Not sure this would work for everyone. But it can help few people like me

GourishettySairam avatar Dec 10 '23 12:12 GourishettySairam