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

132
Visualizações
Having trouble converting this require statement into an import statement

I have a require statement in my script.

const packageJsonVersion = require('../package.json').version;

If I try and run the script I get an error saying I need to convert it to an import statement.

I changed the code to import { version as packageJsonVersion } from '../package.json' but when I run the script I get the following error.

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /home/alex/_code/connect/package.json

I'm not sure how to get around this.

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

0

It depends on what environment you're doing this in.

  • Right now, browsers don't natively support JSON modules (though it's coming).
  • If you're doing this with a bundler, you need to be sure to tell the bundler how to handle the .json extension.
  • If you're doing this in Node.js, you need the JSON module notation (assert {type: "json"}).

Separately, though, JSON modules won't support named exports (this is mentioned by both links above). You'll have to import the default and then use the version property on it:

import data from "../package.json" assert { type: "json" };
const packageJsonVersion = data.version;
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