Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda