6th exercise of learnyounode (using a module to console.log output) - got stuck on stupid stupid thing - > fewer arguments than expected

6th exercise of learnyounode (using a module to console.log output) - got stuck on stupid stupid thing - > fewer arguments than expected
0.0 0


Hey Campers,

So I’m doing the 6th exercise of learnyounode and i got the correct output in test, but it wanted me to pass more arguments:

Your submission results compared to the expected:

             ACTUAL                                 EXPECTED                


“LICENCE.md” == “LICENCE.md”
“README.md” == “README.md”
"" == “”


✓ Submission results match expected

✓ Additional module file exports a single function

✗ Your additional module file [fifth.js] exports a function that takes
fewer than three arguments. You must accept a directory, a filter and a

 # FAIL  
 Your solution to MAKE IT MODULAR didn't pass. Try again! 

My files look like this:

The module (fifth.js):

var fs = require(‘fs’);

function callback (err, list) {
if (err) return console.error(err);
var regexp = “.” + process.argv[3];
function goodExt (element) {

    return element.match(regexp);

var filtered = list.filter(goodExt);
for (var i = 0; i < filtered.length; i++) {


module.exports = function exportThat () {
fs.readdir(process.argv[2], callback);

The main file:

var mymodule = require(’./fifth.js’);

Now, i know that i didn’t handle the errors of the fs.readdir yet, that is the second thing i had problems with. But i wanted to cope with that thing first:
Where do i need to accept those arguments? In mymodule() call or in the exportThat() wrapper function? - because i get weird errors when i try both. I’m lost here.

I get that i can access functions from different files with modules, i feel like this exercise is kind of messy.

Any hint appreciated!

Instructions for the exercise:


MAKE IT MODULAR (Exercise 6 of 13)

This problem is the same as the previous but introduces the concept of
modules. You will need to create two files to solve this.

Create a program that prints a list of files in a given directory,
filtered by the extension of the files. The first argument is the
directory name and the second argument is the extension filter. Print the
list of files (one file per line) to the console. You must use
asynchronous I/O.

You must write a module file to do most of the work. The module must
export a single function that takes three arguments: the directory name,
the filename extension string and a callback function, in that order. The
filename extension argument must be the same as what was passed to your
program. Don’t turn it into a RegExp or prefix with “.” or do anything
except pass it to your module where you can do what you need to make your
filter work.

The callback function must be called using the idiomatic node(err, data)
convention. This convention stipulates that unless there’s an error, the
first argument passed to the callback will be null, and the second will be
your data. In this exercise, the data will be your filtered list of files,
as an Array. If you receive an error, e.g. from your call to
fs.readdir(), the callback must be called with the error, and only the
error, as the first argument.

You must not print directly to the console from your module file, only
from your original program.

In the case of an error bubbling up to your original program file, simply
check for it and print an informative message to the console.

These four things are the contract that your module must follow.

» Export a single function that takes exactly the arguments described.
» Call the callback exactly once with an error or some data as described.
» Don’t change anything else, like global variables or stdout.
» Handle all the errors that may occur and pass them to the callback.

The benefit of having a contract is that your module can be used by anyone
who expects this contract. So your module could be used by anyone else who
does learnyounode, or the verifier, and just work.



Create a new module by creating a new file that just contains your
directory reading and filtering function. To define a single function
export, you assign your function to the module.exports object, overwriting
what is already there:

 module.exports = function (args) { /* ... */ }  

Or you can use a named function and assign the name.

To use your new module in your original program file, use the require()
call in the same way that you require(‘fs’) to load the fs module. The
only difference is that for local modules must be prefixed with ‘./’. So,
if your file is named mymodule.js then:

 var mymodule = require('./mymodule.js')  

The ‘.js’ is optional here and you will often see it omitted.

You now have the module.exports object in your module assigned to the
mymodule variable. Since you are exporting a single function, mymodule is
a function you can call!

Also keep in mind that it is idiomatic to check for errors and do
early-returns within callback functions:

 function bar (callback) {  
   foo(function (err, data) {  
     if (err)  
       return callback(err) // early return  
     // ... no error, continue doing cool things with `data`  
     // all went well, call callback with `null` for the error argument  
     callback(null, data)  


Your current problem is that you exported function currently accepts 0 arguments. The arguments you require your exported function to handle should go in the parentheses:

function myFunction(arg1, arg2, arg3) {
  // do stuff


Yeah i tried it, but i don’t get it here, why do i need these arguments if i get correct results without them?

and after:

module.exports = function exportThat (path, dir, cb) {
fs.readdir(process.argv[2], callback);

i get this:

your additional module file [fifth.js] did not call the callback
argument after an error from fs.readdir()

I’m totally lost here, How is it suppose to call the callback after an error from fs.readdir()? Where? readdir doesn’t even have a body,


where do you assign value to path, dir, cb? Currently they don’t mean anything to your function.


I see i even got the arguments names not right.

Actually, i watched this walkthrough to clear things out for me. https://www.youtube.com/watch?v=yHL6mEr-LGY#t=627.926512

I guess it was confusing to me for many reasons, one of which was that i already had a callback called ‘callback’ :smiley:

I didn’t get though , why did i have to return callback(err) if there was an error with readdir, an not just console.error(err) (?)


I’m not sure :slight_smile:

There 's probably some ‘best practice’ pro reason for doing it that way, but I don’t know what it is!

Make it modular was one of the harder nodeschool lessons for me to complete, too, but making modular scripts is a godsend when you get into the backend API projects.

Congrats on moving on :slight_smile: