1. 程式人生 > 實用技巧 >問題 TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'

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

問題

提示:這個 Bug 可能會隨版本更新而消失。

使用 yarn create react-app my-app --template typescript 後,執行 yarn start 時出現的錯誤。

yarn run v1.22.5
$ react-scripts start
C:\Users\Liang\Desktop\Project\i-show-you\webapp\node_modules\react-scripts\scripts\utils\verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'
    at verifyTypeScriptSetup (C:\Users\Liang\Desktop\Project\i-show-you\webapp\node_modules\react-scripts\scripts\utils\verifyTypeScriptSetup.js:239:43)
    at Object.<anonymous> (C:\Users\Liang\Desktop\Project\i-show-you\webapp\node_modules\react-scripts\scripts\start.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

參考

在 GitHub Issue 中找到參考:TypeError: Cannot assign to read only property 'jsx' of object '#<Object>' · Issue #10110 · facebook/create-react-app

原因

可能與 tsconfig.json 中的設定有關。

解決方案

其中的一種解決方案如下,需要依次執行下面三條命令:

yarn add [email protected]
rm tsconfig.json
yarn start

如果你用的是 Windows 的話,可能在第二步,要手動刪除一下 tsconfig.json 檔案。

其他解決方案可以參考上面的 Issue。