0

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.

2
  • 2
    The error is on line 27 in module.js yet you only show up to line 18. If that's your entire file, you're missing closing parenthesis, otherwise, you should at least post the few lines around the error. Commented Jan 16, 2016 at 15:24
  • You were right, closing parenthesis were indeed missing. Ans as gnerkus said, 'module' should have been './module' in the main file. Thank you! Commented Jan 16, 2016 at 15:40

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

You are right! The code is now perfecly working. Thank you very much! I edited the first message with the right version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.