#JSON import raises typescript error

1 messages · Page 1 of 1 (latest)

hollow idol
#

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:

  1. Run bun init -y
  2. Replace the contents of index.ts with 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

  1. Added "resolveJsonModule": true to compilerOptions in
  2. Added "include": ["*"] to tsconfig

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);
sharp spade
hollow idol
#

Great question!

upper karma
#

Any update on this?

sharp spade
#

weird hack

cerulean hedge