typeError: Cannot assign to read only property 'paths' of object for compilerOptions in tsconfig
Asked Answered
C

3

6
**Trying to set the path inside tsconfig.json for compiler to treat src as baseUrl in react typescript project**

  {
  "compilerOptions": {
    "target": "es5",
    "baseUrl": "src",
    "paths": {
      "*": ["src/*"]
    }
    "include": [
       "src"
     ]
  }

But getting error as below:

TypeError: Cannot assign to read only property 'paths' of object '#<Object>'
    at verifyTypeScriptSetup (/Users/apple/Documents/projects/my-app/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)

Is this because of the folder permission or anything missing in the configuration

Conformance answered 29/10, 2020 at 14:38 Comment(0)
P
10

Apparently, there's a bug in [email protected].

The workaround that solved for me was:

Downgrade react-scripts:

yarn add [email protected]

Create a paths.json in the project's root:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@Components/*": ["components/*"]
    }
  }
}

Add an extends to tsconfig.json:

{
  "extends": "./paths.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"
  },
  "include": [
    "src"
  ]
}
Pandanus answered 2/11, 2020 at 16:29 Comment(3)
Thanks, wasted so much time on thisScopp
You saved me so much time thank you. UPVOTED. but they should fix thisPompano
Seems fixed in version 4.0.1Tried
O
2

Per the GitHub issue, if you just delete the tsconfig.json file then run "npm start" it will recreate the tsconfig.json file for you and everything will work fine.

Osullivan answered 22/11, 2020 at 20:34 Comment(0)
E
1

I fixed it with the following change:

As was previously mentioned, deleting your tsconfig and letting it auto-create a default one also fixes the issue. If you're like me, though, you may have been alarmed for a second that you can only use the default config, with the workaround breaking your base path for absolute imports. (not so - phew!)

Specifically, the setting I had that was breaking post-upgrade was compileOnSave, which is no longer necessary to specify.

enter image description here

Essie answered 4/1, 2021 at 21:57 Comment(1)
Thank you @DrShaffopolis!! Recently I have upgraded "react-scripts" to "^4.0.1", and making changes as proposed above worked for me. +1Linin

© 2022 - 2024 — McMap. All rights reserved.