create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

4.0.0 breaks with typescript (all versions)

Open benneq opened this issue 6 years ago • 117 comments

Describe the bug

I made a clean install of cra@next with typescript and then upgraded typescript to 4.1.0-beta ( also tested 4.1.0-dev.20201023 )

npx create-react-app@next --scripts-version=@next --template=typescript@next my-ts-app

$$$ then replaced typescript 4.0.3 with 4.1.0-beta ( or ^4.1.0-beta ) in package.json

rm -rf node_modules package-lock.json && npm i

npm start

Error message:

node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'

( EDIT: This doesn't happen with cra 4.0.0-next.98 )

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

I just looked for typescript bugs reported and closed in the last few days

Environment

System: OS: macOS 10.15.7 CPU: (8) x64 Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz Binaries: Node: 14.14.0 - /usr/local/bin/node Yarn: Not Found npm: 6.14.8 - /usr/local/bin/npm Browsers: Chrome: 86.0.4240.111 Firefox: Not Found Safari: 14.0 npmPackages: react: ^17.0.1 => 17.0.1 react-dom: ^17.0.1 => 17.0.1 react-scripts: 4.0.0-next.117 => 4.0.0-next.117 npmGlobalPackages: create-react-app: Not Found

Steps to reproduce

  1. npx create-react-app@next --scripts-version=@next --template=typescript@next my-ts-app

  2. replace typescript 4.0.3 with 4.1.0-beta ( or ^4.1.0-beta ) in package.json

  3. rm -rf node_modules package-lock.json && npm i

  4. npm start

Expected behavior

Should work out of the box

Actual behavior

node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'

Reproducible demo

Steps are simple enough to build your own demo

benneq avatar Oct 23 '20 10:10 benneq

There is another similar issue with updating the TypeScript config file. We're still trying to determine the root cause. In this particular case you can work around it by manually setting jsx: react-jsx in tsconfig.json.

iansu avatar Oct 23 '20 14:10 iansu

@iansu That doesn't work.

I did the same steps as above, and put "jsx": "react-jsx" in tsconfig.json.

I get the same error when running npm start

benneq avatar Oct 23 '20 14:10 benneq

That was working for me. You can also try deleting your tsconfig.json and it should automatically get recreated with the new settings.

iansu avatar Oct 23 '20 14:10 iansu

First attempt:

  1. npx create-react-app@next --scripts-version=@next --template=typescript@next my-ts-app
  2. edit package.json: set "typescript" version to ^4.1.0-beta
  3. edit tsconfig.json: set "jsx" to "react-jsx"
  4. rm -rf node_modules package-lock.json && npm i
  5. npm start

Error:

node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'

Second try: deleting tsconfig.json before running npm start. Works! But why?

Here's the recreated tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

That looks exactly the same as the file I had before 😕

benneq avatar Oct 23 '20 14:10 benneq

I don't know why just changing that value wasn't working for you. This is an annoying bug that we're hoping to have fixed soon.

iansu avatar Oct 23 '20 14:10 iansu

@iansu I found it!!

I started debugging verifyTypSscriptSetup.js

First issue I found: (line 151)

    jsx: {
      parsedValue:
        hasJsxRuntime && semver.gte(ts.version, '4.1.0-beta')
          ? ts.JsxEmit.ReactJsx
          : ts.JsxEmit.React,

It must be ts.JsxEmit.ReactJSX (capital letters) instead of ts.JsxEmit.ReactJsx.

Perfect example why people use TypeScript instead of plain old JS. 😏

benneq avatar Oct 23 '20 15:10 benneq

You're right about the capitalization but I don't know if that's the root issue. For example, if you change moduleResolution to classic you'll get the same error and that definitely used to work.

iansu avatar Oct 23 '20 15:10 iansu

If you'd like to submit a PR to fix that casing issue that would be appreciated.

iansu avatar Oct 23 '20 15:10 iansu

Already done: https://github.com/facebook/create-react-app/pull/9869

benneq avatar Oct 23 '20 15:10 benneq

For example, if you change moduleResolution to classic you'll get the same error and that definitely used to work.

True. But at least I can now restart my app without always deleting tsconfig.json

Btw: I doesn't matter what you change. You don't have to change anything at all. Just save the file as is, and you'll get the error.

benneq avatar Oct 23 '20 15:10 benneq

More Information:

Object.isFrozen(appTsConfig) // false
Object.isFrozen(appTsConfig.compilerOptions) // true
Object.isFrozen(appTsConfig.compilerOptions[option]) // true

That would explain why the property isn't writable.

How about a simple workaround that creates a new object instead of modifying the parsed data?

benneq avatar Oct 23 '20 16:10 benneq

I think I got it! (lines 173 to 194)

    const { config: readTsConfig, error } = ts.readConfigFile(
      paths.appTsConfig,
      ts.sys.readFile
    );

    console.log(Object.isFrozen(readTsConfig.compilerOptions)); // false

    appTsConfig = readTsConfig;

    parsedTsConfig = immer(readTsConfig, config => {
      result = ts.parseJsonConfigFileContent(
        config,
        ts.sys,
        path.dirname(paths.appTsConfig)
      );
    });

    console.log(Object.isFrozen(readTsConfig.compilerOptions)); // true

Guess who's bad...

Again: Wouldn't have happened with TypeScript code 😼

Immer automatically freezes any state trees that are modified using produce.

source: https://immerjs.github.io/immer/docs/freezing

I guess the author of these lines knows why he used immer and how to fix this 😄 Commit: https://github.com/facebook/create-react-app/commit/315ff4b4874806f0a9526f8ce79cd82bffc8bec6#diff-2f231dbdc363c929e899c94ae0d999f9886fdc6e33fb88d498a6b101a4bf9f68

@ianschmitz Your help would be appreciated

benneq avatar Oct 23 '20 16:10 benneq

I have come across the same issue, I triaged it the exact same way all the way down to immer.

I think the easiest solution here is to just not use immer... It's creating an immutable data structure, but then following code attempts to mutate it 🤷‍♂️.

jamesmfriedman avatar Oct 23 '20 20:10 jamesmfriedman

This resolves the issue, although removes the reason to use immer... Plop this at the top of the file.

const setAutoFreeze = require('react-dev-utils/immer').setAutoFreeze;
setAutoFreeze(false);

jamesmfriedman avatar Oct 23 '20 20:10 jamesmfriedman

But maybe this causes other issues. I guess there's reason why @ianschmitz introduced immer there.

benneq avatar Oct 23 '20 20:10 benneq

@benneq just to be clear, you might want to update the title of this issue, I have verified:

  • This breaks with ANY version of TS
  • This happens anytime one of your local config params doesn't exactly match what create-react-app spits out by default.
  • Simple repro, just set "paths" to anything, and watch it fail.
  • Another repro, as you have stated, try setting "jsx": to "react-jsx"

I think this is a critical bug for anyone trying to migrate over.

jamesmfriedman avatar Oct 23 '20 20:10 jamesmfriedman

See my answer here for a temporary fix. Also kinda related to #9429.

NearHuscarl avatar Oct 24 '20 12:10 NearHuscarl

@NearHuscarl Unfortunately this temporary fix only works if at the end of the day you are using the default TS config in your project. Our project brakes because we use baseUrl: "./src", and we can't hotfix it... :(

PupoSDC avatar Oct 24 '20 12:10 PupoSDC

having the same problem, with TS 4.1.0-beta, yarn build works fine, but yarn start gives me

node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'

DenysVuika avatar Oct 24 '20 14:10 DenysVuika

Same problem. Is it mentioned somehow in the upgrade plan?

ievgennaida avatar Oct 26 '20 08:10 ievgennaida