Open alamothe opened 2 years ago
Does this import work?
import { ReactComponent as Logo } from "./Logo.svg";
I should add that importing { ReactComponent as x}
is the approach I've always taken, based on the documentation for importing SVGs
The same error happens for me even when importing with { ReactComponent as x}
.
I’ve tried to minimize my SVG file with scour but the import still fails.
I am also unable to use an SVG import with { ReactComponent as x }
or a regular direct import.
My SVG contains CDATA which resulted in this error:
Module build failed (from ./node_modules/@svgr/webpack/lib/index.js): Error: Expected node, got
followed by a large block of CDATA.
Removing the CDATA block from the SVG file resulted in the same error @alamothe reported.
I was using one of the unDraw SVGs and got this error.
But I noticed that I modified it using Inkscape and the modified version wasn't working but the original version downloaded from the site worked.
So I took a look at the files and saw that the original one was way more compressed/compact so I decided to take the one that wasn't working and compress it and see if that worked. It works!
Compress your SVGs using this site
It worked for me. After compressing it using the site (default settings) I stopped having this error. And saved a few bytes!
:+1: if it works for you!
Guys, if possible let's keep this bug for regular import:
import Logo from './logo.svg'
This is quite different than importing as a component (both have different trade-offs).
For regular import, CRA shouldn't be trying to make it as a component in the first place, it is to be used inside img
tag.
Same issue. Is there a possible fix yet?
Same issue. Is there a possible fix yet?
I provided 1 above. it might work for you. read up
Same issue. Is there a possible fix yet?
I used svgo as a node CLI workaround.
Did the trick for me. YMMV.
Same issue. Is there a possible fix yet?
I provided 1 above. it might work for you. read up
Thanks, it worked like a charm!
Quickest way to temporarily workaround this bug is to reconfigure the import itself in the fly taking advantage of webpack being used under the hood.
I won’t go into detail of the technique; please refer for webpack config docs for the details and possible other variants of the change. (You could go even for something more wholesome like CRA overrides based on this etc.)
🏃♂️ TL;DR (example)
Instead of
import Logo from "./logo.svg";
you should just use
import Logo from "!file-loader!./logo.svg";
(’till the bug is resolved).
⚠ Warning (Jest)
See @mykter answer below for solving possible issues with Jest.
None of the solutions above worked for me. I am using animated SVG's which may be a different issue all together.
I have this issue too
I fixed this issue by modifying the naming scheme of the attributes within the svg file to camel case. For example:
xmlns:xlink
becomes xmlnsXlink
xml:space
becomes xmlSpace
After resolving this syntax error, the importing { ReactComponent as x}
approach mentioned above successfully rendered the svg. For reference, the fix can be found originally on this StackOverflow answer.
I was using one of the unDraw SVGs and got this error.
But I noticed that I modified it using Inkscape and the modified version wasn't working but the original version downloaded from the site worked.
So I took a look at the files and saw that the original one was way more compressed/compact so I decided to take the one that wasn't working and compress it and see if that worked. It works!
Potential solution:
Compress your SVGs using this site
It worked for me. After compressing it using the site (default settings) I stopped having this error. And saved a few bytes!
👍 if it works for you!
Same issue, work for me ! Thanks
I was using one of the unDraw SVGs and got this error. But I noticed that I modified it using Inkscape and the modified version wasn't working but the original version downloaded from the site worked. So I took a look at the files and saw that the original one was way more compressed/compact so I decided to take the one that wasn't working and compress it and see if that worked. It works!
Potential solution:
Compress your SVGs using this site It worked for me. After compressing it using the site (default settings) I stopped having this error. And saved a few bytes! 👍 if it works for you!
Same issue, work for me ! Thanks
This worked perfectly for me!! Thank you!!
Is it a viable solution to manually change the content of the SVG files?
I have over 100 SVGs in my assets
folder. Changing them one by one is not an option in my case.
Is it a viable solution to manually change the content of the SVG files? I have over 100 SVGs in my
assets
folder. Changing them one by one is not an option in my case.
svgo runs from the command line. It will process multiple files. It's not a solution, but a viable workaround.
SVGO didn't worked for me, but the import with file-loader
did the trick
I accidentally excluded the react-app-env.d.ts
file from my repo. Adding it back solved my issues with svg files.
For those trying svgo solution try run svgo -r .
from root of your project.
https://www.svgminify.com/ works for me too!
If you're using the import Logo from "!file-loader!./logo.svg";
workaround and are using Jest for tests, you might get an error like:
Cannot find module '!file-loader!./logo.svg' from ...
To avoid this you can mock out static assets in your tests as described here: https://jestjs.io/docs/webpack
The maintainer can probably allow us to configure this behavior by turning it on and off using environment variables. https://create-react-app.dev/docs/advanced-configuration/ Please allow us to configure this.
Quickest way to temporarily workaround this bug is to reconfigure the import itself in the fly taking advantage of webpack being used under the hood.
I won’t go into detail of the technique; please refer for webpack config docs for the details and possible other variants of the change. (You could go even for something more wholesome like CRA overrides based on this etc.)
🏃♂️ TL;DR (example) Instead of
import Logo from "./logo.svg";
you should just use
import Logo from "!file-loader!./logo.svg";
(’till the bug is resolved).
⚠ Warning (Jest) See @mykter answer below for solving possible issues with Jest.
Doesn't work!
Unexpected '!' in '!file-loader!../../assets/header.svg'. Do not use import syntax to configure webpack loaders
Also when running tests:
Cannot find module '!file-loader!../../assets/header.svg' from 'src/main.ts'
@fix-me for the first issue you can suppress the ESLint error with a comment like // eslint-disable-next-line
prior to the import.
For the test issue, see the link in the text you quoted: https://github.com/facebook/create-react-app/issues/11770#issuecomment-1068117768
I can t believe people are still looking for inappropriate workarounds for this bug. This needs to be fixed and while a fix is done you can downgrade react scripts for a while.
There are two solutions: A: Rollback to react-scripts 4.0.3 and react-error-overlay 6.0.9 B: Switch to Nextjs tooling, https://colinhacks.com/essays/building-a-spa-with-nextjs https://nextjs.org/docs/advanced-features/static-html-export
There are two solutions: A: Rollback to react-scripts 4.0.3 and react-error-overlay 6.0.9 B: Switch to Nextjs tooling, https://colinhacks.com/essays/building-a-spa-with-nextjs
These are not really solutions, since the no longer maintained version is not passing WhiteSource anymore and Azure is moving forward dropping support for node < 14 soon. Switching to another framework for a 100k lines of code thing is also not an option, sry.
There are two solutions: A: Rollback to react-scripts 4.0.3 and react-error-overlay 6.0.9 B: Switch to Nextjs tooling, https://colinhacks.com/essays/building-a-spa-with-nextjs
These are not really solutions, since the no longer maintained version is not passing WhiteSource anymore and Azure is moving forward dropping support for node < 14 soon. Switching to another framework for a 100k lines of code thing is also not an option, sry.
The second solution doesn't change the code, you can add an spa entrypoint to nextjs and it will treat it like a pure spa.
This ticket is open for four months. Is there any progress from official side to expect?
Thank you!
Zero progress on 5.0.1 since 5.0.0 was out, unfortunately.
Are there any news regarding this issue with react-scripts > 4.0.3 ?
I use several SVGs and animated SVGs, for me is enough to import them as an img (not as a reactComponent).
Thanks in advance. Luis
For me, i had issue with animating svgs, that i was importing and using like this
import MastHeadSVG from './assets/MastHead.svg'
export const MastHead = () => {
return (
<Root>
<object type='image/svg+xml' data={MastHeadSVG}>
Masthead animation
</object>
</Root>
)
}
What worked was to have a yarn resolution
for @svgr.webpack
at version 6.2.1
. Reason is that react-scripts
is using version 5.*.*
and some aspects of the animation were not supported from what i gathered
in package.json
"resolutions": {
"@svgr/webpack": "6.2.1"
},
You need to remove node_modules and re-install again
Hope that helps someone
For me, i had issue with animating svgs, that i was importing and using like this
import MastHeadSVG from './assets/MastHead.svg' export const MastHead = () => { return ( <Root> <object type='image/svg+xml' data={MastHeadSVG}> Masthead animation </object> </Root> ) }
What worked was to have a
yarn resolution
for@svgr.webpack
at version6.2.1
. Reason is thatreact-scripts
is using version5.*.*
and some aspects of the animation were not supported from what i gatheredin
package.json
"resolutions": { "@svgr/webpack": "6.2.1" },
You need to remove node_modules and re-install again
Hope that helps someone
That sounds promising so far, thank you! I will give it a try. I didn't think about adding some resolutions. Maybe it can be addressed to the maintainers to use the proper version?
it didn't work for me
for me neither
just for additional info, you might need to remove the previous node_modules
and maybe the .lock
file for the resolution to work
Seems like this is a common issue, being able to use SVGs should be assured functionality. Is there something behind the scenes that is blocking this from being worked on?
Not sure if it's relevant as I do not use create-react-app, but I came across this problem earlier and solved it.
In short, my setup uses webpack and is configured to load SVGs with `@svgr/webpack' (default settings).
This morning, I made some tests with the svgo
CLI. In that purpose, I have created a svgo.config.js
file in my home directory.
From then, building my react app fails with the Namespace tags are not supported by default.
error.
The point is @svgr/webpack
uses the config file from my home directory instead of its own svgo config.
My ~/svgo.config.js file was like
module.exports = {
plugins: [
],
};
Renaming my svgo.config.js
file to my_svgo.config.js
solved the problem.
Not sure if it's relevant as I do not use create-react-app, but I came across this problem earlier and solved it.
In short, my setup uses webpack and is configured to load SVGs with `@svgr/webpack' (default settings).
This morning, I made some tests with the
svgo
CLI. In that purpose, I have created asvgo.config.js
file in my home directory.From then, building my react app fails with the
Namespace tags are not supported by default.
error.The point is
@svgr/webpack
uses the config file from my home directory instead of its own svgo config.My ~/svgo.config.js file was like
module.exports = { plugins: [ ], };
Renaming my
svgo.config.js
file tomy_svgo.config.js
solved the problem.
The config files are capsuled by react-scripts and you normally don't need to adjust them.
it didn't work for me
I think the suggestion using resolution / overrides as stated here https://github.com/facebook/create-react-app/issues/11770#issuecomment-1114173305 only works with npm version 8+ which is part of node 16+. Unfortunately we have node 14 in our project, so this is also no option for us yet. But this thing is interesting and powerful to force updates for nested dependencies of unmaintained / deprecated libraries.
I hope 5.0.2 with a fix will be released soon 🥴 The feature branch is stall and this is really needed for production to fix react-scripts 4 dependency check with evidence count > 11
For me, i had issue with animating svgs, that i was importing and using like this
import MastHeadSVG from './assets/MastHead.svg' export const MastHead = () => { return ( <Root> <object type='image/svg+xml' data={MastHeadSVG}> Masthead animation </object> </Root> ) }
What worked was to have a
yarn resolution
for@svgr.webpack
at version6.2.1
. Reason is thatreact-scripts
is using version5.*.*
and some aspects of the animation were not supported from what i gathered inpackage.json
"resolutions": { "@svgr/webpack": "6.2.1" },
You need to remove node_modules and re-install again
Hope that helps someone
That sounds promising so far, thank you! I will give it a try. I didn't think about adding some resolutions. Maybe it can be addressed to the maintainers to use the proper version?
For me, i had issue with animating svgs, that i was importing and using like this
import MastHeadSVG from './assets/MastHead.svg' export const MastHead = () => { return ( <Root> <object type='image/svg+xml' data={MastHeadSVG}> Masthead animation </object> </Root> ) }
What worked was to have a
yarn resolution
for@svgr.webpack
at version6.2.1
. Reason is thatreact-scripts
is using version5.*.*
and some aspects of the animation were not supported from what i gatheredin
package.json
"resolutions": { "@svgr/webpack": "6.2.1" },
You need to remove node_modules and re-install again
Hope that helps someone
Works for me with npm overrides
"overrides": {
"@svgr/webpack": "6.2.1"
},
For me, i had issue with animating svgs, that i was importing and using like this
import MastHeadSVG from './assets/MastHead.svg' export const MastHead = () => { return ( <Root> <object type='image/svg+xml' data={MastHeadSVG}> Masthead animation </object> </Root> ) }
What worked was to have a
yarn resolution
for@svgr.webpack
at version6.2.1
. Reason is thatreact-scripts
is using version5.*.*
and some aspects of the animation were not supported from what i gathered inpackage.json
"resolutions": { "@svgr/webpack": "6.2.1" },
You need to remove node_modules and re-install again
Hope that helps someone
That sounds promising so far, thank you! I will give it a try. I didn't think about adding some resolutions. Maybe it can be addressed to the maintainers to use the proper version?
For me, i had issue with animating svgs, that i was importing and using like this
import MastHeadSVG from './assets/MastHead.svg' export const MastHead = () => { return ( <Root> <object type='image/svg+xml' data={MastHeadSVG}> Masthead animation </object> </Root> ) }
What worked was to have a
yarn resolution
for@svgr.webpack
at version6.2.1
. Reason is thatreact-scripts
is using version5.*.*
and some aspects of the animation were not supported from what i gathered inpackage.json
"resolutions": { "@svgr/webpack": "6.2.1" },
You need to remove node_modules and re-install again
Hope that helps someone
Works for me with npm overrides
"overrides": { "@svgr/webpack": "6.2.1" },
these require node 16+ with npm 8+
Is it a viable solution to manually change the content of the SVG files? I have over 100 SVGs in my
assets
folder. Changing them one by one is not an option in my case.
Thank you! Everyone's talking about modifying the SVG itself which to "fix the problem" rather than talking about why we need to do this now? I don't want to have to manage my SVGs as JSX documents!
Possible solution in the way: #12646. Kudos to @lucabattistini! 🙇♂️
We got it fixed by modifying the webpack configuration with react-scripts-rewired and passing the svg through the babel loader before svgr webpack and file loader.
Still an issue for us, trying to migrate from v4 to v5. It seems that svgr does not transform SVG files to react component properly, it retains xmlns:dc
and the likes rather than changing them to the corresponding prop name. This seems to be a tricky thing to do though.
Any word on #12646 getting merged and 5.0.2 being released. This is really too bad because I would really like to upgrade to 5 but this is breaking my workflow.
I agree with @JoshMcCullough above!
Is it a viable solution to manually change the content of the SVG files? I have over 100 SVGs in my
assets
folder. Changing them one by one is not an option in my case.Thank you! Everyone's talking about modifying the SVG itself which to "fix the problem" rather than talking about why we need to do this now? I don't want to have to manage my SVGs as JSX documents!
Describe the bug
When importing a SVG in a regular manner (not as a component):
There is an error upon
yarn start
:This seems like a regression from CRA 4 which didn't have this problem. I understand the error is relevant if I tried to import it as a component, but that's not what's happening here.
Did you try recovering your dependencies?
yes
Which terms did you search for in User Guide?
SyntaxError: unknown: Namespace tags are not supported by default
Environment
The expected behavior
Lie in CRA4, there shouldn't be an error.