简体   繁体   中英

Node.js export a function error

Im a new to writing node.js modules . I am writing a module in thw following manner ,

a.js

var fs = require("fs") ;
var util = require("util") ;
var mime = require("mime") ;

module.exports = {
  getDataUri: function (image, callback ) {
    var data = fs.readFileSync(image).toString("base64") ;
    var dataUri =  util.format("data:%s;base64;%s", mime.lookup(image), data) ;
    return callback(dataUri) ;
  }
};

b.js

var d = require('./a.js') ;

function getDiff(image1, image2, callback) {
  d.getDataUri(image1, function (d1) {
    d.getDataUri(image2, function (d2) {
      if (d1==d2) {
        return callback(true) ;
      }
      else {
        return callback(false) ;
      }
    }) ;
  }) ;
} ;

getDiff('/Users/sarthakmunshi/Desktop/Print/27274510-Geek-head-logo-template-Stock-Vector.jpg ',
'/Users/sarthakmunshi/Desktop/Print/27274510-Geek-head-logo-template-Stock-Vector.jpg ', function(res) {
  console.log(res) ;
}) ;

However, when i run b.js i get a weird error ,

$ node b.js

    fs.js:438
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT, no such file or directory '/Users/sarthakmunshi/Desktop/Print/27274510-Geek-head-logo-template-Stock-Vector.jpg '
    at Object.fs.openSync (fs.js:438:18)
    at Object.fs.readFileSync (fs.js:289:15)
    at Object.module.exports.getDataUri (/Users/sarthakmunshi/Desktop/Print/all-image/lib/dataUri.js:11:19)
    at getDiff (/Users/sarthakmunshi/Desktop/Print/all-image/lib/comparison.js:4:5)
    at Object.<anonymous> (/Users/sarthakmunshi/Desktop/Print/all-image/lib/comparison.js:16:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

I am unable to rectify this error . need assistance .

Sorry for trouble, I solved this issue by eliminating the space at the end of the path . I feel stupid .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM