question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVGR generates invalid react component

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:15
  • Comments:21

github_iconTop GitHub Comments

9reactions
stenzingcommented, Feb 27, 2022

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

8reactions
damozhangcommented, Dec 15, 2021

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/ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

SVG with React via svgr failing - Stack Overflow
In your webpack.config.js: { test: /\.svg$/, use: ['@svgr/webpack', 'url-loader'], }. In your code: import starUrl, { ReactComponent as Star } ...
Read more >
Options - SVGR
Modify all SVG nodes with uppercase and use a specific template with react-native-svg imports. All unsupported nodes will be removed.
Read more >
error: objects are not valid as a react child (found ... - You.com
This example is for a functional component with hooks but can be adapted for class components. const svg = useRef(null); ...
Read more >
DOM Elements - React
This applies to all regular DOM and SVG elements like <div> , <a> , and others. If you use React with Web Components...
Read more >
gatsby-plugin-react-svg
It's likely that you use SVG in your React component, that is processed by url-loader instead of svg-react-loader due to incorrect configuration. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found