1

I simply can't import json files in a NodeJS v18 project. I am using TS 4.8.4 and i have in the tsconfig.json

"compilerOptions": {
  "esModuleInterop": true,
  "target": "esnext",
  "lib": ["esnext"],
  "module": "commonjs",
  "resolveJsonModule": true,
  "forceConsistentCasingInFileNames": true,
  "strict": true,
  "skipLibCheck": true
}

In index.ts i have:

import * as json from '../config.json';
console.log(json);

I already saw many answers in stackoverflow like this one but i can discover what seems to be the problem. Any suggestions? Thank you.

EDIT: I keep going in circles. The TS compile gives errors and suggest to change the module. I change the module than the import does not anymore. I can't find a solution for this.

UPDATE Changing the module to nodenext in the tsconfig, the import of the json file works BUT the import of other TS files do not work anymore: error TS2835: Relative import paths need explicit file extensions in

EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './myclass.js'?

What else do i need to change now? Thank you.

1 Answers1

0

In the import, you have mentioned "import * as json from '../config.json';" instead of that try "import * as json from './config.json';" Or you can also try using absolute path for the json just for testing purpose.

Vikrant Korde
  • 315
  • 2
  • 11