Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda