Skip to content

upgrading to react-script 3.3.1 breaks optional chaining #8445

Closed

Description

@ggascoigne

Since upgrading to react-scripts 3.3.1 my code, which uses optional chaining, fails to compile.

Module parse failed: Unexpected token (22:12)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
 * ./node_modules/eslint-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
|   // library barfs if you have an undefined value here instead of an empty array
|
>   if (props?.SelectProps?.multiple && field.value === undefined) {
|     field.value = [];

Activity

  1. lnhrdt commented on Feb 9, 2020

    @lnhrdt

    @ggascoigne I encountered the same problem when upgrading to 3.3.1. I was able to resolve the issue and stay on 3.3.1 by deleting my node_modules folder and reinstalling my dependencies.

    I don't understand the underlying issue but sharing this info in case it helps others.

  2. JeromeDeLeon commented on Feb 9, 2020

    @JeromeDeLeon
    Contributor

    Removing node_modules didn't work in my case or perhaps this is not related to my question regarding calling function with ?. in it, like so, getSum?.()?

    ESLint complains about Expected an assignment or function call and instead saw an expression. eslint(no-unused-expressions)

    PS: Disabling eslint in my VSCode worked.

  3. yilinjuang commented on Feb 9, 2020

    @yilinjuang

    @lnhrdt removing node_modules works for me. Thanks.

  4. lvpro commented on Feb 10, 2020

    @lvpro

    Same issue here. Removing node_modules did not help.

    We're ejected, though, so I added @babel/plugin-proposal-nullish-coalescing-operator to our .babelrc file myself, then it started working again.

  5. PatrickZGW commented on Feb 12, 2020

    @PatrickZGW

    Removing node_modules did not work for me. Our project is not ejected.

  6. hanneswidrig commented on Feb 15, 2020

    @hanneswidrig

    This issue is still present on 3.4.0.

  7. justingrant commented on Feb 19, 2020

    @justingrant
    Contributor

    Looking at the diff of 3.3.0 vs. 3.3.1, I'm guessing that #8353 (FYI @ianschmitz) may be the culprit because it removed the babel plugins for optional chaining, null coalescing, and dynamic import.

    To verify that this was the culprit, I manually added the optional chaining and null-coalescing babel plugins to CRA 3.4.0 using customize-cra. When I did this, npm start no longer reported any errors from use of optional-chaining and null-coalescing operators.

    Until this is fixed, here's workaround instructions that worked for me for those two operators:

    1. Follow the instructions in https://medium.com/@adostes/enabling-optional-chaining-in-a-create-react-app-a9f626a515d9 to install customize-cra and configure it to add optional chaining via a custom .babelrc. This step should get optional chaining working. The next steps will get null-coalescing working too.
    2. npm i -D @babel/plugin-proposal-nullish-coalescing-operator
    3. Add null-coalescing plu to .babelrc. You should end up with a .babelrc that looks like this:
    // .babelrc
    {
      "plugins": [
        "@babel/plugin-proposal-optional-chaining",
        "@babel/plugin-proposal-nullish-coalescing-operator"
      ]
    }

    FYI: I couldn't get customize-cra's AddBabelPlugin method to work, but useBabelRc() seems to work fine so I didn't spend time to figure out what's going wrong with AddBabelPlugin().

  8. ianschmitz commented on Feb 19, 2020

    @ianschmitz
    Contributor

    @justingrant would you be able to upload your lock file here by chance?

    It's tricky to debug as a fresh app doesn't reproduce:
    image

  9. justingrant commented on Feb 19, 2020

    @justingrant
    Contributor

    @ianschmitz - Sure, here's my package-lock.json: https://gist.github.com/justingrant/cb9b3c088f90abd8382141ebe608f141

    In case it's helpful, here's my package.json too (with private stuff removed): https://gist.github.com/justingrant/194b21ccea3510ee8ef972c0cd9d8ca4

    FWIW, I tried rebuilding my node_modules and that didn't fix the problem. I get the same problem on 3.3.1 and 3.4.0.

    Also, while researching this problem I found a similar issue in the webpack repo: webpack/webpack#10227. I'm not sure if this is the same root cause because that issue uses ts-loader for typescript but my app uses babel. But at least wanted to capture it here to save others some Googling in case it's related. That repro seems to depend on target: "ESNext", which made me wonder if it could be related to my dev-mode browserslist config in package.json:

      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version"
        ]
      }

    Here's two other issues which I came across which also might be (or might not be!) related to this issue:

  10. ianschmitz commented on Feb 19, 2020

    @ianschmitz
    Contributor

    Interesting! Can you try updating your browserslist dev settings to include ie 11 as a test and see if the issue persists? You may have to clear out node_modules after doing so as sometimes the babel cache can persist (at least in past this happened).

  11. ianschmitz commented on Feb 19, 2020

    @ianschmitz
    Contributor

    I think i've tracked down the source of the issue. It looks like we will require acornjs/acorn#890 and acornjs/acorn#891 to be merged and included by webpack before we can fully delegate the decision to transform optional chaining and nullish coalescing to browserslist completely. This syntax isn't yet understood by acorn which webpack uses to parse the source. I'll test this more later to confirm when i get the time.

    Assuming the above is the case, we will add the plugins back in the next patch release until we get a newer version of webpack that will support the syntax. Until then a quick fix could be to update your browserslist config to include chrome 79 if you were using an aggressive list like the last 1 chrome version example above, as it sounds like support was added in version 80: https://caniuse.com/#search=optional%20chaining.

  12. justingrant commented on Feb 19, 2020

    @justingrant
    Contributor

    @ianschmitz - Sorry for delayed response. Looks like you already identified the root cause. Do you still need me to test ie 11? Your proposed diagnosis and solution sounds right, BTW. I did confirm that changing browserslist without clearing node_modules does NOT solve the problem, at least in my app.

    Also, where does the babel cache live? Can I just delete it, instead of all of node_modules? I'd love to avoid re-creating node_modules every time I want to change my browserslist!

  13. ianschmitz commented on Feb 19, 2020

    @ianschmitz
    Contributor

    It should live at node_modules/.cache. Hopefully that's still the case. We've documented it in our docs to help out: https://create-react-app.dev/docs/supported-browsers-features/#configuring-supported-browsers.

    I haven't had a chance to verify this in-depth, so would appreciate to get additional verification from you folks before we restore the plugins. Thanks! 😄

  14. yordis commented on Feb 19, 2020

    @yordis

    Confirmed in our end that removing node_modules because of the Babel cache works (we are not using CRA but we are using the babel preset)

  15. justingrant commented on Feb 20, 2020

    @justingrant
    Contributor

    @ianschmitz - I can confirm that adding chrome 79 to browserslist causes the problem to go away after deleting node_modules/.cache, quitting the dev server, and restarting it with npm start.

  16. JeromeDeLeon commented on Feb 20, 2020

    @JeromeDeLeon
    Contributor

    I can confirm it. Same as @justingrant did

  17. ianschmitz commented on Feb 20, 2020

    @ianschmitz
    Contributor

    Perfect. Thanks everyone. Will have a PR fix up shortly.

  18. added this to the 3.4.1 milestone on Feb 20, 2020
  19. locked and limited conversation to collaborators on Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions