Open alamothe opened 2 years ago
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!
Looking at this repo activity it seems this project is abandoned. Too bad no public statement is made and that gives people hopes about a solution.
I've successfully migrated from CRA to vite months ago, it went smoothly and everything works nice. Made it in one afternoon. I recommend doing the same.
Hello all, I'm trying to use an SVG from SVGator in my react project but keep coming across the following error:
Module build failed (from ./node_modules/react-scripts/node_modules/@svgr/webpack/lib/index.js):
Error: Expected node, got J
I read through this thread but see no conclusive solution. Has anyone been able to find a fix to this?
The SVG File has the following:
<svg id="eDWYMRjh2Sd1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 640 480" shape-rendering="geometricPrecision" text-rendering="geometricPrecision">
<polygon points="65.75,22.72 34.25,22.72 18.5,50 34.25,77.28 65.75,77.28 81.5,50 65.75,22.72"
transform="matrix(1.139208 0 0 1.139208 263.0396 183.0396)" fill="#64ffda" fill-opacity="0"
stroke="#64ffda" stroke-width="2.5" />
<text dx="0" dy="0" font-family=""Roboto"" font-size="50" font-weight="400"
transform="translate(306.205945 257.510468)" fill="#64ffda" stroke-width="0">
<tspan y="0" font-weight="400" stroke-width="0"><![CDATA[J]]></tspan>
</text>
</svg>
The error goes away when I remove <![CDATA[j]]>
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!
This ended up working for me, had the same issue on version 18.2.0:
Module build failed (from ./node_modules/@svgr/webpack/lib/index.js):
SyntaxError: unknown file: Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can set `throwIfNamespace: false` to bypass this warning.
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!
Looking at this repo activity it seems this project is abandoned. Too bad no public statement is made and that gives people hopes about a solution.
I've successfully migrated from CRA to vite months ago, it went smoothly and everything works nice. Made it in one afternoon. I recommend doing the same.
Yes, thanks @wmitsuda, I also migrated to Vite. It's great!
what is wrong in this code-\ it gives an error of ### react-dom.development.js:16317 Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
`import logo from './logo.svg'; import './App.css'; import Login from './components/Login'; import Enroll from './components/Enroll'; import Home from './components/Home'; import react, { useState } from 'react';
function App() { const [isLogin, setIsLogin] = useState(false); const [isEnroll, setIsEnroll] = useState(false); const [isOld, setIsOld] = useState(""); const [isOld2, setIsOld2] = useState("");
setIsOld(localStorage.getItem('password'))//if you have anything in local
setIsOld2(localStorage.getItem('name'))
return (
); }
export default App;`
this is the return in was droped
return (
);
again..
@Bat-Sheva-Sh very off-topic for this thread, but setting state will cause a re-render. As you set state every single render, every render will trigger a re-render, causing your infinite loop. Maybe only update your state values if they are out of date.
so what actually should i change/ drop?
For react-native app, https://github.com/kristerkari/react-native-svg-transformer solve it
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!
+1 if it works for you!
Thanks, It was helpful
For those that don't mind using CRACO, a sample craco.config.js for those that only use SVG imports in <img src=.../>
and not as a component looks like so:
module.exports = {
webpack: {
configure: (webpackConfig, { env, paths }) => {
// Fix CRA #11770
const rules = webpackConfig.module.rules;
for (const rule of rules) {
if (Object.hasOwn(rule, 'oneOf')) {
rule.oneOf.filter((currentValue, index, arr) => {
const toRemove =
currentValue.test instanceof RegExp && currentValue.test.test('something.svg');
if (toRemove) {
arr.splice(index, 1);
}
return toRemove;
});
rule.oneOf.push({
test: /\.svg$/i,
issuer: {
and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
},
type: 'asset',
});
}
}
return webpackConfig;
},
},
};
If you have different needs, you can try adding the other svg
rule from #12646 with the ?url
query parameter.
Removing the tag
<![CDATA[
...
]]>
and keeping the content in between, worked for us!
I fixed this issue by modifying the naming scheme of the attributes within the svg file to camel case. For example:
xmlns:xlink
becomesxmlnsXlink
xml:space
becomesxmlSpace
- etc.
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.
This worked for me, thank you!
<svg enable-background="new 0 0 32 32" height="32px" id="Layer_1" version="1.0" viewBox="0 0 32 32" width="32px" xml:space="preserve" // xmlSpace="preserve" -- just remove colon and write in camelCase xmlns="http://www.w3.org/2000/svg" // xmlns:xlink="http://www.w3.org/1999/xlink" // xmlnsXlink="http://www.w3.org/1999/xlink" -- -- just remove colon and write in camelCase
// Svg Code
This worked for me. 😁 Before
After
This is still an issue for me. I'm trying to upgrade from CRA 4 -> 5 but encountering this problem. Yes I know 2 known workarounds is:
!file-loader!./img.svg
But in my case, I am receiving my SVG from my company's private npm repository so I'm unable to modify the SVG. I am also unable to solve it using the 2nd option with the file-loader, because who knows, it just don't seem to work on npm modules I guess?
I wish this bug would be resolved. Using workarounds don't feel like a solution. Namespaces are a part of the SVG-specification so I believe it should be supported.
How can I help?
@SimpleCookie Unfortunately I think the best option might be workaround number 3, which is to migrate away from CRA.
With the end of life here, this bug may never see a fix; instead getting entombed with the dead project.
I donʼt see any end-of-life notice on this project… Meta representative could put one here to be fair with all the users (or at least us).
excuses for bringing your attention here, @gaearon: any progress or at least plans to address this?
enable svgo, work for me
@huoguozhang: enable svgo, work for me
huh? not sure if i should bother to ask, where did you set that exactly…? 🤔 (looks at least like a repetition of mathburnham and Poyoman39 answers)
if you use Inkscape, an alternative to running the svg through a compressor is to save it as an "optimised svg": this worked for me.
@SimpleCookie Unfortunately I think the best option might be workaround number 3, which is to migrate away from CRA.
With the end of life here, this bug may never see a fix; instead getting entombed with the dead project.
This is the solution we took. We migrated from CRA. I'm happy with the result.
This worked for me.
Animated SVG is working as expected.
In my case, I used animated SVG exported from Svgator that includes CDATA.
I have not done any changes to SVG file like remove/modifying CDATA. I have not done any changes to webpack config or so.
Simply tried the code from below pic.
Environments: "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "^5.0.1",
This worked for me.
In my svg file.
I did this -
bx:guide TO bxGuide
xmlns:bx TO xmlnsBx
bx:fonts TO bxFonts
Basically, change into camelCase.
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.