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.

TSX files in adjacent yarn workspace packages result in Module parse failed: Unexpected token. You need an appropriate TypeScript loader

See original GitHub issue

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

  1. Initialize a yarn monorepo
  2. Create a react app as a package in that monorepo with the typescript template create-react-app web --template typescript
  3. Create a package components alongside the previously created web package and add typescript and a tsconfig.json
  4. Add the components package to web’s dependencies.
  5. In src/components create an index.ts file with a default export function that logs something to the console (i.e. no JSX).
  6. Import that function in web and log to the console at run time.
  7. Experience no error.
  8. Change the src/components/index.ts to src/components/index.tsx and make the function run some JSX.
  9. Update web to render the exported JSX function.
  10. 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:

  1. git checkout bug
  2. yarn install
  3. cd packages/web
  4. 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:closed
  • Created 3 years ago
  • Reactions:39
  • Comments:29 (1 by maintainers)

github_iconTop GitHub Comments

39reactions
iansucommented, May 19, 2021

Importing uncompiled code from other packages in a monorepo is not currently supported by Create React App.

29reactions
Chanducommented, Jun 20, 2020

I am using craco and this is what worked for me:

// File craco.config.js
const CracoLessPlugin = require('craco-less');
const path = require('path');
module.exports = {
  plugins: [
    {
      plugin: {
        overrideWebpackConfig: ({ webpackConfig }) => {
          const oneOfRule = webpackConfig.module.rules.find(
            (rule) => rule.oneOf,
          );
          if (oneOfRule) {
            const tsxRule = oneOfRule.oneOf.find(
              (rule) => rule.test && rule.test.toString().includes('tsx'),
            );

            const newIncludePaths = [
              // relative path to my yarn workspace library  
              path.resolve(__dirname, '../../libs/domain/'),
            ];
            if (tsxRule) {
              if (Array.isArray(tsxRule.include)) {
                tsxRule.include = [...tsxRule.include, ...newIncludePaths];
              } else {
                tsxRule.include = [tsxRule.include, ...newIncludePaths];
              }
            }
          }
          return webpackConfig;
        },
      },
    },
  ],
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module parse failed: Unexpected token (257:106) You may ...
I overwrote the new package-lock.json with old one and my react app built and ran successfully again. I then changed (downgrade) some of...
Read more >
Module parse failed unexpected token tsx - hs-barssel.de
I recently upgraded from Webpack 17 Haz 2021 npx create-react-app my-app --template typescript; yarn add @viselect/react; Import and use the lib in App....
Read more >
A guide through The Wild Wild West of setting up a mono repo ...
In this part, I will explain step by step how to setup Lerna, Yarn Workspaces and Typescript in a mono repo. In the...
Read more >
Webpack With Multiple Typscript Projects In Yarn Workspaces
Module parse failed : Unexpected token.You may need an appropriate loader to handle this file type currently no loaders are configured to process...
Read more >
Configuring Jest
The configuration file should simply export an object: JavaScript; TypeScript. /** @type {import('jest').Config} */
Read more >

github_iconTop Related Medium Post

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