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

170
Visualizações
Object loaded from json file has its values assigned to a property called default

In my node + TypeScript project, I have a json file with some default config. Let's say it looks like this:

{
  prop1: 123,
  prop2: 123
}

And I'm loading values from this file somewhere along the way like this:

import * as defconfig from '../configuration/config.json';

And when I'm checking it in the console I see this output:

{
  prop1: [Getter],
  prop2: [Getter],
    default: {
      prop1: 123,
      prop2: 123
   }
}

And also if I try to override this object with spread operator or Object.assign, the result is following:

const options = {
  prop1: 321,
  prop2: 321
};

console.log({...defconfig, ...options});

//OR

console.log(Object.assign(defconfing, options));

//produces

{
  prop1: 321,
  prop2: 321,
  default: {
    prop1: 123,
    prop2: 123
  }
}

Can anyone explain where is the default coming from and how to get rid of it?

The same code works perfectly fine when not using .json file but defining config with a normal object, but the issue is that I have to use the file.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to the latest node versions, JSON files cannot be read directly as modules. Unless you use --experimental-json-modules flag while running.

A solution would be to use commonjs (for that you would need to change file extension to .cjs) method require which will be able to do the task or you can use fs module to read JSON string and then parse.

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