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

210
Visualizações
Get String from local SVG file in nodejs

I'm trying to get a String of an SVG file.

What I wanna get:

"<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"

What I've tried:

const svgFile = await fsPromises.readFile(`./icons/${directoryName}/${iconName}`)
const serializer = new XMLSerializer()
const svgString = serializer.serializeToString(svgFile)
console.log(svgString)

Output: ReferenceError: XMLSerializer is not defined

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

0

It seems that you are trying to use browser classes in node. Unless you are using another framework, such as Electron, this isn't possible out of the box.

However, there are often some libraries that can act as drop-in replacement.

For XMLSerializer, I found https://www.npmjs.com/package/xmlserializer.

If you are doing no further processing, you can also just directly read it to a string.

const svgString = await fsPromises.readFile(`./icons/${directoryName}/${iconName}`, "utf8");

Unlike the other answer, this is shorter.

about 4 years ago · Juan Pablo Isaza Relatório

0

I found a pretty simple solution. Reading the file returns a Buffer which can then be converted to a String.

const svgFileData = await fsPromises.readFile(`./icons/${directoryName}/${iconName}`)
console.log(svgFileData.toString())
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