javascript - Modules and Node.js -
i'm trying learn node.js basics doing learnyounode lessons. stuck @ make modular chapter.
i don't know if i'm allowed paste content here, roughly, asked create module filter file names in directory following specified extension.
here i've done far:
var module = require('module'); module(process.argv[2], process.argv[3], function (err, data){ if(err){ console.log("error"); } else{ console.log(data); } });
module.js:
var fs = require('fs'); var path = require('path'); module.exports = function (dir, ext, callback){ fs.readdir(dir, function (err, files){ if(err){ callback(err); } else { var array = ''; for(var i=0; i<files.length; i++){ if(path.extname(files[i]) === '.'+ext){ array+=files[i]+"\n"; } } callback(null, array); } }); }
i following error:
module.js:27 this.id = id; ^ typeerror: cannot set property 'id' of undefined @ module (module.js:27:11) @ object.<anonymous> (/home/maxence/documents/node.js/learnyounode/mim/mim.js:3:1) @ module._compile (module.js:397:26) @ object.module._extensions..js (module.js:404:10) @ module.load (module.js:343:32) @ function.module._load (module.js:300:12) @ module.require (module.js:353:17) @ require (internal/module.js:12:17) @ object.<anonymous> (/usr/local/lib/node_modules/learnyounode/node_modules/workshopper-wrappedexec/exec-wrap.js:87:3) @ module._compile (module.js:397:26)
my guess i've not declared variable correctly somewhere in code can't manage find it. call of module correct?
best regards, mm
edit : gnerkus said, there should './module' instead of 'module'. here working code:
var module = require('./module'); module(process.argv[2], process.argv[3], function (err, data){ if(err){ console.log("error"); } else{ console.log(data); } });
module.js:
var fs = require('fs'); var path = require('path'); module.exports = function (dir, ext, callback){ fs.readdir(dir, function (err, files){ if(err){ callback(err); } else { var array = ''; for(var i=0; i<files.length; i++){ if(path.extname(files[i]) === '.'+ext){ array+=files[i]+"\n"; } } callback(null, array); } }); }
edit 2: seems version preferred:
var module = require('./module'); module(process.argv[2], process.argv[3], function (err, data){ if(err){ console.log("error"); } else{ for(var i=0; i<data.length; i++){ console.log(data[i]); } } });
module.js:
var fs = require('fs'); var path = require('path'); module.exports = function (dir, ext, callback){ fs.readdir(dir, function (err, files){ if(err){ callback(err); } else { var array = []; for(var i=0; i<files.length; i++){ if(path.extname(files[i]) === ('.'+ext)){ array.push(files[i]); } } callback(null, array); } }); }
both correct, second version required complete chapter.
the error occurs because you're requiring node module , not custom module. nodejs documentation:
if module identifier passed require() not native module, , not begin '/', '../', or './', node.js starts @ parent directory of current module, , adds /node_modules, , attempts load module location. node not append node_modules path ending in node_modules.
solution.js
var mymodule = require('./module'); // rest of code.
Comments
Post a Comment