question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Babel JSX Syntax error when importing from a linked library

See original GitHub issue

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

(Write your answer here if relevant.)

Environment

Running npx create-react-app --info does not seem to work. Getting:

Please specify the project directory:
  create-react-app <project-directory>

For example:
  create-react-app my-react-app

Run create-react-app --help to see all options.

However I am using: Node v8.11.3 Yarn 1.9.4 npm 6.4.1 react-scripts 2.0.0-next.2150693d

Steps to Reproduce

  1. Create a new Create React App (The actual App)
  2. Create another Create React App (The “Library”)
  3. Link them together: Import a component from the Library into the App

This used to work with react-scripts 2.0.0-next.a671462c

Expected Behavior

The app should compile just fine as it used to in 2.0.0-next.a671462c

Actual Behavior

Application fails to compile with “Syntax error: Unexpected token” in the component, that was attempted to be imported from the other application.

screen shot 2018-09-26 at 16 36 00

To me it seems like the babel loader is somehow not transpiling the imported component.

Reproducible Demo

https://github.com/FelixKuehl/cra-alpha-issue-example

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:26 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
earGOcommented, Sep 9, 2019

We’ve had quite some time figuring this one out, but I think we found the reason and solution. Check this out guys - I bet you’ll be blown away.

So, when CRA tries to transpile your package, it uses babel to transpile JSX code to commonjs. Last versions of babel transpilers have big troubles with unnamed exports. So when you try and import your default as unnamed export (for example export default YourComponent), it throws an error.

But we do not see this error, 'cause of CRA bundle. This error shows in console, for example, when we try and configure babel with webpack in monorepo by hand.

The solution, that worked for me and my colleagues is this one:

  1. make package structure like this
YourPackage
|-src
|   |-index.js
|-index.js
  1. Export your component or module code in /src/index.js as a default export (export default YourComponent)

  2. In root index.js export your component as follows export { default as YourComponent } from './src/index.js'

  3. you can import your component in consumer app like this: import {YourComponent} from YourPackage (plus the namespace, of course)

And that’s it. This worked for us beautifully.

Special thanks to this hero, https://github.com/aivtel who actually solved the problem. My part was in pointing where to look and why this unfortunate issue was happening in the first place.

8reactions
kode8commented, Aug 10, 2020

Spent a day to work out this heap of crap. Why is the React eco system so f__ked? Back to Vue I go.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Babel/JSX Transpiler error on build when importing a module ...
You're correct, the issue is that the react-collision-provider hasn't been properly transpiled to be understood by the browser.
Read more >
Troubleshooting | React Navigation
I'm getting "SyntaxError in @react-navigation/xxx/xxx.tsx" or ... Linking is automatic from React Native 0.60, so if you have linked the library manually, ...
Read more >
Introducing the New JSX Transform – React Blog
With the new transform, you can use JSX without importing React. ... This upgrade will not change the JSX syntax and is not...
Read more >
babel/preset-react
Note: Flow syntax support is no longer enabled in v7. ... "classic" // defaults to classic // "importSource": "custom-jsx-library" // defaults to react...
Read more >
Getting Started · React Native Paper
The plugin automatically rewrites the import statements so that only the modules you use are imported instead of the whole library. Add react-native-paper/babel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found