Search code examples
reactjsbabeljsbabel-loader

React: Support for the experimental syntax 'jsx' isn't currently enabled


I have written a node module but when I try to use it in a react app created using create-react-app I get this error when I run react-scripts start or react-scripts build:

ERROR in ./node_modules/@xxx/react-file-input/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /mnt/c/Users/xxx/file-picker-demo/node_modules/@xxx/react-file-input/index.js: Support for the experimental syntax 'jsx' isn't currently enabled (40:11):

  38 |     if (maxFileSize && maxFileSize > 0 && file.size > maxFileSize) {
  39 |       return (
> 40 |           <span>{String.fromCharCode(9888)}</span>
     |           ^
  41 |       );
  42 |     }
  43 |   };

Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.

The error does NOT happen if I copy and paste the code into the main application and do not import the node module.

Is there some config file that I need to add to my node module? I looked here and tried adding .babelrc, babel.config.js, webpack.config.js to my node module and none of them makes the error go away. How can I fix this?

Further details: I am using react-scripts 5.0.0. I don't have any babel or webpack config files in my project (create-react-app does not generate any). I understand react-scripts has an internal configuration for babel and webpack that it uses.


Solution

  • JSX has to be compiled via Babel to convert it into a form that browsers can understand. React does this compilation for JSX files in the main application but does not process JSX files in the main app's dependencies. So the only solution is to build your node module using Babel and perhaps webpack or rollup. Refer below links for how to do that:

    After that you can import the module and there will be no error.