create-react-app: Minified React error #152 - comments before JSX
Since CRA 3.4.1 Probably a babel issue
The following code raise an error on built version :
import React from 'react';
const Component = props => {
return ( // a comment
<div>
toto
</div>
);
};
export default Component;
After building the app, i get the following error :
react-dom.production.min.js:209 Error: Minified React error #152; visit https://reactjs.org/docs/error-decoder.html?invariant=152&args[]=Component for the full message or use the non-minified dev environment for full errors and additional helpful warnings. at react-dom.production.min.js:149 at za (react-dom.production.min.js:173) at vo (react-dom.production.min.js:262) at cu (react-dom.production.min.js:246) at ou (react-dom.production.min.js:246) at Zo (react-dom.production.min.js:239) at qo (react-dom.production.min.js:230) at Du (react-dom.production.min.js:281) at react-dom.production.min.js:284 at tu (react-dom.production.min.js:240)
Removing the comment fixes the error
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 144
- Comments: 58 (1 by maintainers)
Links to this issue
Commits related to this issue
- fix: solve build error no.152 https://github.com/facebook/create-react-app/issues/8687 — committed to lbwa/how-virtual-scroll-works by lbwa 4 years ago
- Workaround for https://github.com/facebook/create-react-app/issues/8687 — committed to nikaspran/suggest-subreddit by nikaspran 4 years ago
- ❌ Avoid Build Error #157 https://github.com/facebook/create-react-app/issues/8687 — committed to scalarhq/videotranscode.space by CryogenicPlanet 4 years ago
- remove comments before JSX hopefully this does it, see here: https://github.com/facebook/create-react-app/issues/8687 — committed to Solaros711/pixelparty by pjz987 4 years ago
- Fix deployment issue where nothing was rendered Seems to be caused by a bug in babel: https://github.com/facebook/create-react-app/issues/8687 — committed to TerrorJacktyl/jinx-it-project by azoricus 4 years ago
- Moved comment outside of the return statement, in an attempt to fix broken transpilation, see https://github.com/facebook/create-react-app/issues/8687. — committed to hajkmap/Hajk by jacobwod 3 years ago
I used this regex in VSCode to find the troublesome comments:
\(\s*\n?\s*//
I was able to resolve mine by removing the comments as well. Not sure of a fix yet.
+1
I can confirm this is true, and it’s only after
return (
, not a variable such asconst elem = (
This causes the error
These do not
Duplicate https://github.com/facebook/create-react-app/issues/8809
I’ve played with my codebase quite a bit over the last hour to see what breaks it. It looks like the code breaks when the comment is at the beginning of the return statement. My code doesn’t seem to care when the comment is anywhere after the first JSX tag. I stand to be corrected though.
Very weird and unexpected, tbh.
I have something like this because of comment, ref is not assigned in production but working fine in development.
Remove comments everything works fine
Wasted 2 hours on this…thanks
+1
I use yarn and I just add a “resolutions” block to my project’s package.json. It works like a charm.
"resolutions": { "@babel/core": "^7.9.6" }
This way, you don’t have to chase all the breaking comments down, though I’m not sure what the repercussions of this might be.
This problem (or at least an aspect of it) seems to be fixed since
@babel/core
7.9.6. (upstream issue babel/babel#11304 could be related, but I’m not sure).Steps to reproduce the fix, based on @antoinerousseau’s repro from https://github.com/facebook/create-react-app/issues/8687#issuecomment-606199085:
I’m ejecting to prove it’s fixed after upgrading
@babel/core
, see question below.IMPORTANT: if you change the build setup, do not forget to
rm -rf node_modules/.cache
before rebuilding. Otherwise some changes will have no effect.I don’t know if other types of comments are fixed, the repro mentioned above tests only
Unfortunatley,
create-react-app
(react-scripts
more specifically) has a fixed dependency"@babel/core": "7.9.0"
and I don’t know how to override this. Any tips appreciated.It’s time for a new CRA release…
Update 1: I tested with code from @hugomallet’s original issue
and this is also fixed with
@babel/core
7.9.6Update 2: according to the latest commits to
create-react-app
, next release will land this babel fix, see for example here (create-react-app
) and here (react-scripts
).The only comments that work are the ones wrapped in brackets. The ones with '// ’ will cause it to break.
Am currently experiencing the same error, tried removing all comments yet the error still occurs.
I implemented a custom
eslint
rule to prevent this.You probably want to apply this only to
.jsx
or.tsx
files, editing your.eslintrc
with something like this:Of course the are pitfalls:
It can be improved but I did not want to spend more time on it.
It helped me prevent breaking my compiled app again (it happened twice before).
I just came across this today. Removing comments solved it.
React Minified error #152
Ran into this issue as well, probably from terser minification.
worked around this by:
to
Happens in 3.4.0 too
I had the same error when having a comment like
/** TODO: *Refactor into a newer component */
Issue is still here. Comments in JSX caused to such a pain. Spent 2 hours, while found this topic, removed all comments from everywhere and - finally magic happens!
I fixed this issue, I missed one of the comments from app. I removed and this issue was fixed.
Thanks @antoinerousseau
Wasted my 3 hours… remove comments works for me
@antoinerousseau Thanks for all the info and the help!!
Well, I didn’t belive that simply removing all the comments would work, but work it! 2 days struggling having same issue of @krittiyaclark! Thank you!