Mithun Shreevatsa
Mithun Shreevatsa

Reputation: 3609

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

**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

Upvotes: 6

Views: 4715

Answers (3)

DrShaffopolis
DrShaffopolis

Reputation: 1098

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

Upvotes: 1

Ryan Kennel
Ryan Kennel

Reputation: 35

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.

Upvotes: 2

lucsrods
lucsrods

Reputation: 166

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"
  ]
}

Upvotes: 10

Related Questions