#Endpoint import json

3 messages · Page 1 of 1 (latest)

jaunty stag
#

Hello, I am trying to import a json file in my SSR astro site endpoint.

this works in dev mode:

 let gamePostsJsonPath = `../../globals/_gamedata/_${language}_${countryVisitor}.json`;
  if (language=='en') {
    gamePostsJsonPath = `../../globals/_gamedata/_${language}_${countryVisitor}.json`;
  }
  // Dynamically import the JSON file
  const gamePostsJsonModule = await import(gamePostsJsonPath);
  // Access the default export which contains the parsed JSON data
  const gamePostsJson = gamePostsJsonModule.default;
  // console.log("loaded");
  // Assign the JSON data to 'finaltable'
  finaltable = gamePostsJson; // 'gamePostsJson' should be an array if the JSON file contains an array

but in production I get a 500 error:

20:26:19 [ERROR] Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/dist/server/globals/_gamedata/_en_US.json' imported from /app/dist/server/chunks/pages/builtwith_C83KLSAu.mjs

surreal fractalBOT
#

Hi @jaunty stag. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It seems like you're encountering an issue with dynamic imports in your Astro endpoint when running in production mode. This could be due to the way your project is built for production, where the file paths might change or files might be moved around, causing the dynamic import to fail.

Astro supports importing JSON files directly into your application as mentioned in the Astro documentation (https://docs.astro.build/en/guides/imports#json). However, the issue you're facing might be related to the file path in the production build.

One of the issues mentioned in the knowledge sources (Github Issue 7033 (https://github.com/withastro/astro/issues/7033)) suggests that certain features like Astro.glob might not work as expected in production due to the way the pre-rendered routes are created and then destroyed. While this is not exactly the same as your issue, it does highlight that there can be differences between development and production environments in Astro.

Unfortunately, without more information or a detailed error message, it's hard to provide a definitive solution. You might want to check the file paths in your production build and ensure that the JSON files are being correctly included and are accessible at the expected paths. If the issue persists, consider creating a minimal reproducible example and reporting it as an issue on the Astro GitHub page for further assistance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: