Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!