Skip to main content
added 32 characters in body
Source Link
Codebling
  • 11.5k
  • 3
  • 45
  • 71

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • base64url (Node v14+)
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • binary/latin1 (ISO8859-1, latin1 only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • binary/latin1 (ISO8859-1, latin1 only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • base64url (Node v14+)
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • binary/latin1 (ISO8859-1, latin1 only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 
edited body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • binary
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • latin1binary/latin1 (ISO8859-1, only in node 6.4.0+latin1 only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • binary
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • latin1 (ISO8859-1, only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • binary/latin1 (ISO8859-1, latin1 only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 
correct version in text as swell
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • binary
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • latin1 (ISO8859-1, only in node 6.4.0+)

If you are using an older version than 8.16.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • binary
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • latin1 (ISO8859-1, only in node 6.4.0+)

If you are using an older version than 8.1.4, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 

The list of encodings that node supports natively is rather short:

  • ascii
  • base64
  • binary
  • hex
  • ucs2/ucs-2/utf16le/utf-16le
  • utf8/utf-8
  • latin1 (ISO8859-1, only in node 6.4.0+)

If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:

Use iconv-lite to recode files:

var iconvlite = require('iconv-lite'); var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); return iconvlite.decode(content, encoding); } 

Alternatively, use iconv:

var Iconv = require('iconv').Iconv; var fs = require('fs'); function readFileSync_encoding(filename, encoding) { var content = fs.readFileSync(filename); var iconv = new Iconv(encoding, 'UTF-8'); var buffer = iconv.convert(content); return buffer.toString('utf8'); } 
edited body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
added 141 characters in body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
clarified which encodings are synonyms
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
Simplify the list - it's not the point of the answer. Also, sort it
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
Updated link to point to master (which will make it more visible if the list changes); made resilient against possibility of total link breakage by copying list of currently-supported encodings into the answer. Feel free to roll back if you disapprove.
Source Link
Mark Amery
  • 158.3k
  • 93
  • 435
  • 476
Loading
edited body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
added 339 characters in body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
added 35 characters in body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
deleted 11 characters in body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
added 1 characters in body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
added 345 characters in body
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading
Source Link
phihag
  • 289.4k
  • 75
  • 475
  • 489
Loading