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.

Cannot compile TypeScript app created with CRA because of parsing errors

See original GitHub issue

Describe the bug

create-react-app seems to have issues compiling valid TypeScript code.

Did you try recovering your dependencies?

Yes, in a number of ways. I cannot manage to fix this, despite spending hours on researching it.

I’ve tried:

  1. Googling (finding semi-similar issues reported, but no answers or solutions)
  2. Removing node_modules and doing npm install and npm ci
  3. Removing package-lock.json and npm install and npm ci
  4. Reconstructing the entire package.json file by doing npm install <package-name> for every single dependency.

Which terms did you search for in User Guide?

parsing error, unexpected token, syntax error, create-react-app, private access modifier, failed to compile, typescript syntax error and tons of permutations of these.

Environment

  • Windows 10 x64
  • node v12.19.0
  • npm 6.14.8

Steps to reproduce

  1. npx create-react-app my-app --typescript
  2. Edit App.tsx and add the following code under the import statements and above the function App() ... line:
class Foo {
  private bar: number;

  constructor() {
    this.bar = 1;
  }
}

Full content of App.tsx:

import React from 'react';
import logo from './logo.svg';
import './App.css';

class Foo {
  private bar: number;

  constructor() {
    this.bar = 1;
  }
}

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;
  1. Run npm run build

NOTE: There are other syntaxes which do not work, such as readonly and more.

Expected behavior

The app is compiled successfully.

Actual behavior

λ npm run build

> my-app@0.1.0 build C:\Users\...\my-app
> react-scripts build

Creating an optimized production build...
Failed to compile.

C:/Users/.../my-app/src/App.tsx
  Line 6:11:  Parsing error: Unexpected token

  4 |
  5 | class Foo {
> 6 |   private bar: number;
    |           ^
  7 |
  8 |   constructor() {
  9 |     this.bar = 1;


npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\...\npm-cache\_logs\2020-10-13T05_11_20_782Z-debug.log

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:32
  • Comments:25

github_iconTop GitHub Comments

88reactions
plumacommented, Oct 26, 2020

Okay, scratch that. After comparing the output of a fresh yarn create react-app with my existing package.json it seems that the file now contains the following section:

  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },

This was missing in the app I upgraded. Presumably this was added to the generator output at some point in the past but inferred if missing and that logic got canned at some point in the 4.0.0 pre-releases, breaking old apps that were missing this bit.

Adding this to my package.json fixed the problem, monorepo or not.

9reactions
tetchelcommented, Mar 22, 2021

for me, react-scripts build was succeeding locally but failing in a docker container. The solution was to copy my .eslintrc.js into the container before doing the build.

My eslintrc does not use react-app or react-app/jest, so it does not seem to be those configs specifically that prevent this issue.

I do not have an eslintConfig section in my package.json.

Bizarre issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript parsing error when EXTEND_ESLINT=true in ...
Afterwards, I get parsing errors on certain (but not all) Typescript grammars. There may be other unrecognised syntaxes I haven't found yet.
Read more >
Using React with TypeScript - Mattermost
This usually occurs because tsconfig.json (TypeScript's configuration file) is absent from your project. CRA should generate this for you, but ...
Read more >
jsconfig.json Reference - Visual Studio Code
Tip: Do not be confused by compilerOptions , since no actual compilation is required for JavaScript. This attribute exists because jsconfig.json is a...
Read more >
ts-loader - npm
As your project becomes bigger, compilation time increases linearly. It's because typescript's semantic checker has to inspect all files on ...
Read more >
Typescript parsing error when EXTEND_ESLINT=true in ...
[Solved]-Typescript parsing error when EXTEND_ESLINT=true in create-react-app-Reactjs ... UPDATE: Update react-scripts to latest version at least 3.4.1. It's ...
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