According to this guide, the following should work:
import data from "./package.json";
console.log(data.name);
While this works While this runs fine, I get the following error from Typescript:
File '/Users/nicholasbaier/astronautical-ascension/package.json' is not listed within the file list of project '/Users/nicholasbaier/astronautical-ascension/tsconfig.json'. Projects must list all files or use an 'include' pattern.ts(6307)
I ran into this issue in a freshly initiated Bun project. To recreate:
- Run
bun init -y - Replace the contents of
index.tswith the above.
For reference, this is the contents of tsconfig.json (completely default)
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"allowImportingTsExtensions": true,
"noEmit": true,
"composite": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": [
"bun-types"
]
}
}
The expected behavior is no typescript error as the one shown above. The code runs fine and is expected to run that way, so the TS error seems anomalous. I have done the following to try to resolve the situation. Neither resolved the issue
- Added
"resolveJsonModule": truetocompilerOptionsin - Added
"include": ["*"]totsconfig
If this is the expected behavior and modifications need to be made to tsconfig.json to allow for JSON imports without throwing an error, the documentation should be updated.
It should also be noted that the following snippet does not result in the same error:
import data from "./test.toml";
console.log(data);