create-react-app: Nullish assignment (??=) not working in new project
Typescript 4 supports the nullish assignment operator (??=). Creating a new Typescript project with CRA 4 supports this operator in the editor, but when running yarn start
it gives a Webpack parsing error.
I asked on SO here and they confirm the bug and suggest it requires a Babel upgrade.
Steps to reproduce:
- Create a new project with Typescript
- Add the following lines into App.tsx:
const test = { a: 1 };
test.a ??= 2;
- Run
yarn start
Expected result: App starts
Actual result:
Module parse failed: Unexpected token (14:10)
File was processed with these loaders:
* ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 43
- Comments: 24
The irony of
Nullish Assignment
being supported in every browser, but breaking via the very thing that’s meant to make future proofing codebases easier.This is possibly now resolved.
i still get this error with react-scripts@4.0.1 installed… i’m interested in tips if anyone has working?
i have craco installed already so went looking for how to enable this in babel and tried simply including
@babel/plugin-proposal-logical-assignment-operators
via craco.config.js but that caused a ton of other errors.im getting a similiar
react-leaflet
issueim using next.js
Reporting a similar error with
react-leaflet
:Out of nowhere, a coworker’s codebase started complaining over
||=
usage - which was working before.It seems related to a recently merged PR where I used NPM v7 by accident (on another machine) and it mixed up lock files… But even though
node_modules
is committed* (so we should have the exact same codebase), mine was working fine but his wasn’t.I even made a fresh clone, and it wasn’t working either. I even compared both
node_modules
in Meld, no relevant changes (besides cache and a log file?). I have no clue why my old clone was working, but I finally gave up on investigating and on postponing “yet another hack on top of a hack” and went with CRACO as well.I have been having this problem when I deleted my package-lock.json in a non-create-react-app project, my suspicions so far are:
Confirmed to not work on none typescript projects as well.
We were able to temporarily solve this issue by using
react-app-rewired
(2.1.8) with@babel/plugin-proposal-logical-assignment-operators
(7.12.1) plugin and the followingconfig-overrides.js
:The config may vary, so you may want to do
console.log(config); exit();
at firstThis issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.
I did upgrade
react-scripts
in my project to@latest
(currently5.0.0-next.47
), and the error is gone. I wish to not meet this issue with another similar error when Babel ^7.16 will be released.I got the same issue when I try to import my package to CRA project
thanks for encouraging me to retry @pandomic… i can confirm corresponding craco config above does work as well
fyi, those previous errors mentioned were from a different issue - additional eslintConfig required after upgrading from react-scripts 3.4.3 to 4