Cannot import json
In wasp 0.16.6 i get the following trying to import json files File '/Users/ertan/Documents/Rezervera/Rezervera/app/src/locales/en/en.json' is not listed within the file list of project '/Users/ertan/Documents/Rezervera/Rezervera/app/tsconfig.json'. Projects must list all files or use an 'include' pattern.ts(6307)
7 Replies
Hi @Ertan Köse. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Wasp currently does not support direct importing of JSON files in your code. Even if you enable TypeScript options like resolveJsonModule and esModuleInterop in your tsconfig.json, Wasp's build process does not take these settings into account. The tsconfig.json file in a Wasp project is used only for IDE support, not for the actual build or compilation process. This is a known limitation, and users have encountered similar issues before. The recommended workaround is to convert your JSON data into a regular JS object and import that instead, or to read and parse the JSON file at runtime if needed.
Here’s a relevant explanation from the Wasp community:
Wasp unfortunately doesn't support importing JSON directly. You'll have to parse it or convert the file to a regular JS object and import that. As for the tsconfig.json - Yes, that should work, but Wasp unfortunately doesn't yet take the user's tsconfig.json into account when processing code. The tsconfig.json file is only there for the IDE support (as explained by the comment on top of the file) Importing a json module - Discord.There is an open issue to improve this and document workarounds, but as of now, direct JSON imports are not supported in Wasp projects Explain in docs how to import JSON files / read static files in Wasp on the server. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Ertan Köse, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Aight so maybe just build the json myself instead of directly loading it? @kapa.ai
mmm i tried it on my machine and yeah, it gives the error, but it runs and builds correctly
@Filip any specific guidance wrt tsconfig complaining about importing json?
Mine fails directly importing the json
Hey @Ertan Köse, I don't think we support importing JSON, let me check
Yes, here's the issue: https://github.com/wasp-lang/wasp/issues/2654
GitHub
Allow users to import
*.json
files · Issue #2654 · wasp-lang/wasptsconfig.json offers a resolveJsonModule option. It's very handy since it allows users to import json files and automatically infers their types. Wasp doesn't currently support it, but we c...
That said, I think I know how you can get it working. It's a hack, but let me know if you're interested and I'll guide you through it
Btw, if your code works, can you just slap a TS ignore on that import? Would that work?