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

238
Visualizações
Typescript dynamic import do not crash/allow compilation on non existing file?

Well I have a system where I have a local input file which has some extra information regarding login etc. Obviously that file isn't in git so it won't be visible for the public when building or even developing on another pc.

I've tried making it hence a dynamic import that is guarded an a try-catch statement, if the file would be there it should just execute on page launch. Otherwise it shouldn't.

So in app.tsx under the main component there is this:

React.useEffect(() => {
  async function loader() {
    if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
      try {
        //@ts-ignore
        const data = await import('./local/input_data');
        //@ts-ignore
        if (data['userLoginData']) {
          //@ts-ignore
          await AuthStoreCtx.authStore?.doLogin({username: data.userLoginData.username, password: data.userLoginData.password});
          if (!AuthStoreCtx.authStore?.loginFailed) {
            console.log("AUTO LOGIN SUCCEEDS");
          } else {
            console.log("AUTO LOGIN FAILED");
          }
          setUser(AuthStoreCtx.authStore?.user);
        }

      } catch (err) {
        console.log("AUTO LOGIN FAIL")
      }
    } else {
      console.log('production')
    }
  }
  if (authStore) {
    loader().then(() => {

    });
  }
}, [authStore]);

However this gives the following error:

Module not found: Error: ...

That is no surprise, but what's surprising is that the error isn't caught by the catch statement (or later by the .catch() outside the promise). How would I do such a thing?

about 4 years ago · Juan Pablo Isaza
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