Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Error: "All files must be modules when the '--isolatedModules' flag is provide" in React with TypeScript

I can't seem to get rid of this error. I'am using create-react-app with TypeScript. Any help, appreciated. Thanks.

All files must be modules when the '--isolatedModules' flag is provided.  TS1208

  > 1 | import randomcolor from 'randomcolor';
      | ^
    2 | import React from 'react';
    3 | import tasksData from '../../assets/data/tasksData';
    4 | import { Task } from '../../model/task';

These are my tsconfig files. I created a base file as the original tsconfig file gets overwritten by the react build scripts.

tsconfig.base.json

{
    "compilerOptions": {
        "isolatedModules": false
    }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ],
  "extends": "./tsconfig.base.json"
}
like image 336
Imran Avatar asked Jul 19 '20 16:07

Imran


People also ask

Why is isolatedmodules not working in react?

However surprisingly the setting of isolatedModules to true doesn't helped. I got the same error too. The reason why this is happening is that the react component you have created is not being used. So you can call the component, and you are good to go. there is no export in the file! for example you can add export default {} to fixed!

Why is my typescript file not showing up as a module?

Show activity on this post. Typescript treats files without import/exports as legacy script files. As such files are not modules and any definitions they have get merged in the global namespace. isolatedModules forbids such files. Adding any import or export to a file makes it a module and the error disappears.

What is isolatedmodules in typescript?

But it can also be used to compile each file separately, what TypeScript calls an ‘isolated module’. That’s what the isolatedModules option (which defaults to false) does.

When do all files have to be modules?

All files must be modules when the '--isolatedModules' flag is provided. Why? What is happening here? What does --isolatedModules actually mean/do? Show activity on this post.


2 Answers

The problem is that in Typescript by default, every file without importor export is a legacy scrip file.

As such files are not modules they are merged in the global namespace.

To change this behavior

a) add any import or export in the file, eg: export {}

b) set "isolatedModules": false in tsconfig.json

Note: NextJS will reset the value to true in every build, because for NextJS, "isolatedModules": true is a requirement

like image 58
Juanma Menendez Avatar answered Oct 26 '22 20:10

Juanma Menendez


May get this error with an unused .tsor .tsx file. I had empty .ts file I wasn't using so just had to delete the unused file.

deleted the unused file /example/index.ts

like image 32
Nick Foden Avatar answered Oct 26 '22 18:10

Nick Foden