TSX files in adjacent yarn workspace packages result in Module parse failed: Unexpected token. You need an appropriate TypeScript loader
See original GitHub issueDescribe 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
components
alongside the previously createdweb
package and add typescript and atsconfig.json
- Add the
components
package toweb
βs dependencies. - In
src/components
create anindex.ts
file with a default export function that logs something to the console (i.e. no JSX). - Import that function in
web
and log to the console at run time. - Experience no error.
- Change the
src/components/index.ts
tosrc/components/index.tsx
and make the function run some JSX. - Update
web
to 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 install
cd packages/web
yarn 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:39
- Comments:29 (1 by maintainers)
Top GitHub Comments
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: