create-react-app: Babel JSX Syntax error when importing from a linked library
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
- Create a new Create React App (The actual App)
- Create another Create React App (The “Library”)
- 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.
To me it seems like the babel loader is somehow not transpiling the imported component.
Reproducible Demo
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 15
- Comments: 26 (4 by maintainers)
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:
Export your component or module code in /src/index.js as a default export (
export default YourComponent
)In root index.js export your component as follows
export { default as YourComponent } from './src/index.js'
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.
Spent a day to work out this heap of crap. Why is the React eco system so f__ked? Back to Vue I go.
I have got the exact issue as @chrisdel101 . Any updates on resolving the compilation error?
Hey @FelixKuehl, we’re really sorry about this! As mentioned in https://github.com/facebook/create-react-app/issues/5024, our monorepo support was incomplete and we think it would create more issues for our users in long term if we shipped it in that state. On the other hand, the 2.x branch has stalled for many months, and we were worried it would never get shipped. So we made a difficult decision to remove some of the features that existed in 2.0 earlier alphas in order to ship a stable 2.0 within two weeks.
We’ll offer migration strategies that you could use to unblock your migration to 2.x, but they’ll likely include introducing another compiler like
nwb
for packages outside the main one. We are also open to more proposals about monorepo support, except that if we land it next time it would need to be a more complete one.Hello all! I have been searching for a solution to my problem, and this seems to be the most relevant. Please let me know if this doesn’t fit with this open issue.
Goals:
I am building two separate repositories. One will be a page which will import other React Components as npm packages once published. So currently I have a local repository that depends on another. For development, I am attempting to link them with
npm link
.Versions:
Circumstances:
.../Code/A/MyComponent/src/TL
.../Code/A/MyApp/src
npm create-react-app MyApp
npm link
, starting withMyComponent
, then runningnpm link MyComponent
inMyApp
MyComponent
node_module inMyApp
.MyComponent
inMyApp
, receivedSyntax error
Unexpected token
at the beginning ofMyComponent
’s JSX code.npm run transpile
inMyComponent
to create adist
directory.npm start
now, I receiv this message: Error: Failed to compile..../MyApp/src/App.js
:Module not found: Can't resolve 'A/MyComponent' in '.../Code/A/MyApp/src'
I tried adding
symlinks: false
here innode_modules/react-scripts/config/webpack.config.js
:This did not help.
Concerns:
This seems to be either a Babel or Webpack issue concerning
npm link
. I imagineMyApp
is either not being pulled together by Webpack and/or not being compiled by Babel.