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"
}
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!
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.
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.
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.
The problem is that in Typescript by default, every file without import
or 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
May get this error with an unused .ts
or .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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With