Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
if I put the path of a json in a const using require, json is not read, but if I put the path of the json directly in the require,is correctly loaded

this is very strange. I am using gatsby, and this code is contained in a functional component. In my real code, depending on a condition a JSON is loaded. the path of the json is correct. I have this code (I have the path of json in a var):

const LoadAnimation = async () => {
    const currentQuizzAnimationFile =
        '../../assets/lottie/Baseball_Loop_Perfect.json';
    const currentQuizzAnimation = await require(currentQuizzAnimationFile);
    console.log(currentQuizzAnimationFile);
};

with previous lines I am getting this problem:

Uncaught (in promise) Error: Cannot find module './../../assets/lottie/Baseball_Loop_Perfect.json'

But if I do (I pass the path of json directly):

//const currentQuizzAnimationFile ='../../assets/lottie/Baseball_Loop_Perfect.json';
const currentQuizzAnimation = await require('../../assets/lottie/Baseball_Loop_Perfect.json');

the json is perfectly read. the problem is that my son may be load by a var

I call my function using a useEffect.

useEffect(() => {
    LoadAnimation();
},[])

If I use Axios, or fetch the result of this json is a empty json.

what is the problem, and how can fix it?

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda