@@ -416,23 +416,35 @@ Module.prototype._compile = function(content, filename) {
416
416
return compiledWrapper . apply ( self . exports , args ) ;
417
417
} ;
418
418
419
+
420
+ function stripBOM ( content ) {
421
+ // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
422
+ // because the buffer-to-string conversion in `fs.readFileSync()`
423
+ // translates it to FEFF, the UTF-16 BOM.
424
+ if ( content . charCodeAt ( 0 ) === 0xFEFF ) {
425
+ content = content . slice ( 1 ) ;
426
+ }
427
+ return content ;
428
+ }
429
+
430
+
419
431
// Native extension for .js
420
432
Module . _extensions [ '.js' ] = function ( module , filename ) {
421
433
var content = NativeModule . require ( 'fs' ) . readFileSync ( filename , 'utf8' ) ;
422
- module . _compile ( content , filename ) ;
423
- } ;
424
-
425
-
426
- // Native extension for .node
427
- Module . _extensions [ '.node' ] = function ( module , filename ) {
428
- process . dlopen ( filename , module . exports ) ;
434
+ module . _compile ( stripBOM ( content ) , filename ) ;
429
435
} ;
430
436
431
437
432
438
// Native extension for .json
433
439
Module . _extensions [ '.json' ] = function ( module , filename ) {
434
440
var content = NativeModule . require ( 'fs' ) . readFileSync ( filename , 'utf8' ) ;
435
- module . exports = JSON . parse ( content ) ;
441
+ module . exports = JSON . parse ( stripBOM ( content ) ) ;
442
+ } ;
443
+
444
+
445
+ //Native extension for .node
446
+ Module . _extensions [ '.node' ] = function ( module , filename ) {
447
+ process . dlopen ( filename , module . exports ) ;
436
448
} ;
437
449
438
450
0 commit comments