Reputation: 1054
I'm trying to learn Node.js basics by doing the learnyounode lessons. I am stuck at the Make it Modular chapter.
I don't know if I'm allowed to paste its content here, but roughly, I am asked to create a module to filter file names in a directory following a specified extension.
Here is what I've done so 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 get the following error:
module.js:27
this.id = id;
^
TypeError: Cannot set property 'id' of undefined
at Module (module.js:27:11)
at Object.<anonymous> (/home/maxence/Documents/Node.js/learnyounode/MIM/mim.js:3:1)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/local/lib/node_modules/learnyounode/node_modules/workshopper-wrappedexec/exec-wrap.js:87:3)
at Module._compile (module.js:397:26)
My guess is I've not declared a variable correctly somewhere in my code but I can't manage to find it. Is the call of the module correct?
Best regards, MM
EDIT : as gnerkus said, there should be './module' instead of 'module'. Here is the 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: it seems that this version is to be 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 are correct, but the second version is required to complete the chapter.
Upvotes: 0
Views: 175
Reputation: 12019
The error occurs because you're requiring a node module and not your custom module. From the NodeJS documentation:
If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.
solution.js
var myModule = require('./module');
// The rest of your code.
Upvotes: 1