create-react-app: TSX files in adjacent yarn workspace packages result in Module parse failed: Unexpected token. You need an appropriate TypeScript loader
Describe the bug
react-scripts cannot load tsx files that are in adjacent yarn workspace packages.
Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type
Did you try recovering your dependencies?
Yes, yarn --version 1.22.4
Environment
Environment Info:
current version of create-react-app: 3.4.1
running from /home/ty/.nvm/versions/node/v10.20.1/lib/node_modules/create-react-app
System:
OS: Linux 5.6 Arch Linux
CPU: (8) x64 Intel(R) Core(TM) i5-8305G CPU @ 2.80GHz
Binaries:
Node: 10.20.1 - ~/.nvm/versions/node/v10.20.1/bin/node
Yarn: 1.22.4 - ~/.nvm/versions/node/v10.20.1/bin/yarn
npm: 6.14.4 - ~/.nvm/versions/node/v10.20.1/bin/npm
Browsers:
Chrome: Not Found
Firefox: Not Found
npmPackages:
react: Not Found
react-dom: Not Found
react-scripts: Not Found
npmGlobalPackages:
create-react-app: 3.4.1
Current behavior
react-scripts can load TypeScript files with ts or tsx extensions when those files are located in the same ‘home’ package as react-scripts (the web package in the demo). react-scripts can load ts files in packages outside of the home package so long as the baseUrl parameter is set to ./ in tsconfig.json. react-scripts cannot load tsx files outside of the home package even if the jsx parameter is set to react.
Steps to reproduce
- Initialize a yarn monorepo
- Create a react app as a package in that monorepo with the typescript template
create-react-app web --template typescript - Create a package
componentsalongside the previously createdwebpackage and add typescript and atsconfig.json - Add the
componentspackage toweb’s dependencies. - In
src/componentscreate anindex.tsfile with a default export function that logs something to the console (i.e. no JSX). - Import that function in
weband log to the console at run time. - Experience no error.
- Change the
src/components/index.tstosrc/components/index.tsxand make the function run some JSX. - Update
webto render the exported JSX function. - Experience the error.
Expected behavior
CRA should be able to import and run JSX (i.e. tsx) code from adjacent packages.
Actual behavior
CRA is only able to import and run ts code and not tsx code.
../components/src/index.tsx 4:2
Module parse failed: Unexpected token (4:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| const App = () => (
> <div>Hello world</div>
| );
|
Attempted Troubleshooting
I tried adding ts-loader using react-app-rewired but that resulted in an error where Typescript was emitting nothing, a result of CRA automatically setting the noEmit flag to true at compile time. Ejecting is a rabbit hole I never want to go down.
Reproducible demo
A demo of the bug is available at woodpav/server-side-react-typescript-monorepo#bug. As described it is a yarn workspace monorepo with two packages: Web and Components. Web contains react-scripts and Components contains JSX components. Both packages have a tsconfig.json that extends tsconfig.base.json.
To run the demo:
- git checkout bug
yarn installcd packages/webyarn start
.
├── package.json
├── packages
│ ├── components
│ │ ├── package.json
│ │ ├── src
│ │ │ └── index.tsx
│ │ └── tsconfig.json
│ └── web
│ ├── node_modules
│ ├── package.json
│ ├── public
│ │ ├── favicon.ico
│ │ ├── index.html
│ │ ├── manifest.json
│ │ └── robots.txt
│ ├── README.md
│ ├── src
│ │ ├── index.tsx
│ │ └── react-app-env.d.ts
│ └── tsconfig.json
├── tsconfig.base.json
└── yarn.lock
7 directories, 15 files
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 42
- Comments: 29 (1 by maintainers)
Commits related to this issue
- Remove react-scripts due to ts monorepos not being supported - https://github.com/facebook/create-react-app/issues/9127 — committed to AriPerkkio/asus-merlin-simple-vpn-client-setup by AriPerkkio 4 years ago
Importing uncompiled code from other packages in a monorepo is not currently supported by Create React App.
I am using craco and this is what worked for me:
Thanks for this, certainly saved me a lot of headache!
Worth noting that in my case (with all latest versions at time of writing) that I did not need to remove the
ModuleScopePluginand that the rule selector needed tweaking. I am using yarn workspaces without Lerna.This is what worked for me in
config-overrides.jswithreact-app-rewired.Thanks 😄
This is what we were asking for. Doesn’t it make sense to leave this open to track it? Unless you’re saying that you’ll NEVER support it and that people needing this should eject or modify the config.
Solved this issue by migrating to vite. It’s also crazy fast compared to cra, so it’s win-win.
@iansu
Well, that’s why we. users, have created this issue report.
I have a working
react-app-rewiredconfig available at woodpav/typescript-monorepo-server-side-react. I maintain that this should be built-in behavior of CRA but until the devs release a fix (or someone writes a PR), you can use the code on #fix to use TypeScript and CRA in a monorepo.Running the demo
The fix is on #fix but checkout #master if you want to experience the bug.
config-overrides.js
EDIT
I original ran into this issue when attempting to create a TypeScript monorepo that uses Server Side Rendering. If you would like to use my boilerplate, clone woodpav/server-side-react-typescript-monorepo run the following commands:
With CRA5 above solution didn’t work for me right away so had to do the following craco.config.js. (Approach is taken from nx setup guide)
And have tsconfig.json paths pointing at shared workspace components like:
For me, this configuration worked:
If anyone else has struggled with this I have a configuration that worked with
react-app-rewiredusing this structure:where I want to import components from storybook to be used in both apps.
Here is the
config-overrides.jsfile:I’m having the same issue, after added my module to webpack config (I’m not using craco, I ejected) it’s giving the following issue:
Looks like the above
react-app-rewiredmethod is not working with the current versions. As ofreact-scripts@4.0.3andreact-app-rewired@2.1.8the solution for me is:config-overrides.js+1, same issue here. Trying to re-use logic from
apipackage in simple yarn workspace project.Having the same issue, even with .ts files, with
baseUrlset to./.I’ve got a Typescript CRA project that has a dependency of a second Typescript CRA project which has
"files": ["src"],in package.json. I’m able to import the types, but this error shows up when I try to import a function.This solution worked for me without craco in my TS CRA
react-rewiredrepoFrom what I understand of this, Yarn workspace packages have trouble importing uncompiled TSX files across packages. So you need to provide paths to include in TSX rules.
@MrBlenny it still works for me, react-scripts@4.0.3