How to migrate from iconv to iconv-lite in NodeJS

Recently I migrate from iconv to iconv-lite in NodeJS, I spent some minutes to migrate it and want to share how to do it in order to help to someone.


Some reasons:

  • Support streaming
  • Performance
  • Don't need native compilation, so it can run on Windows machines like Azure


Changing a few lines:

  • Removing the BOM character
  • Change encode function, on each line if proceed


Let's see an example and how to upgrade it.

Original iconv code example:

// iconv example
const Iconv = require("iconv");
const iconv = new Iconv('utf8', 'utf16le');
res.write(new Buffer([0xff, 0xfe]));
res.write(iconv.convert("your string"));

Now migrated to iconv-lite

// iconv-lite example
const iconv = require("iconv-lite");
res.write(iconv.encode("your string", 'utf8'));

That's all! Leave a comment with your doubts or experience 🤙

David Burgos

Read more posts by this author.