How to include a custom file in the project?
Hey, I need to store a custom .json file in the "server" folder of the Wasp project, what's the correct way to do it? The file is being ignored by the compiler and not present in .wasp folder
4 Replies
Hey @maksym36ua ! So what are you trying to do -> you want to import that file in the code via
import myData from "./some/path/to/my/json/file"
and use it, or do you want to have it on the disk as a static file that you will at some point read with readFile
and do stuff with it?
If you are going for the first thing, then you can't add it and import it as .json because our import system on the backend/node currently doesn't support it, but you can save it .js file, export JSON object from it and normally import it that way via import
.
Btw, file is there -> I just tried and I can confirm it is being copied to the .wasp/out/server/src/ext-src/ . But as I mentioned above, you can use import
on .json
file, it will say it can't find it.
If you want to have the file as "static asset", and read it with readFile
-> I am sure you could do that, you just need to provide correct path. I don't think this is Wasp specific though -> so you would figure out how to determine the correct path from the node file, or maybe you know where you put the file in some other way, ... .
I hope this helps!Possibly useful doc: https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/ .
How to import JSON files in ES modules (Node.js)
Learn about the ways to read and import JSON files in Node.js ECMAscript modules (ESM).
>If you are going for the first thing, then you can't add it and import it as .json because our import system on the backend/node currently doesn't support it, but you can save it .js file, export JSON object from it and normally import it that way via import.
This is exactly what I ended up doing 🙂 Thanks! 🙌
Awesome!