Welcome to the Linux Foundation Forum!

Node readdir undefined path error problem (nodeschool make it modular)

Hello, I have a question that is not directly from the course material but I think it might be interesting.

I've been doing some nodeschool challenges and got stuck with make it modular challenge from learnyounode module. In order to pass the challenge I have to write a script that takes two arguments - the first one is the directory path and the second one file extension (without '.') - and then prints out all the files with given extension in a given directory.

The script has to contain two files (hence the name - make it modular). Here's my code:


make-it-modular.js


The script does what is supposed to do but I can't validate the solution (the validation is an automated nodeschool script) because I get the following error:

Your additional module file [mymodule.js] does not appear to pass back an
error received from fs.readdir(). Use the following idiomatic Node.js
pattern inside your callback to fs.readdir(): if (err) return
callback(err)

In the beginning I thought this was a bug because when calling the script with faulty arguments i.e.

node make-it-modular.js 2 js

the error was passed back. But then I realized that if the script is called without arguments (node make-it-modular.js) it throws the following error (which indeed is not passed to the main script):

internal/fs/utils.js:634
throw new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
at readdir (fs.js:1018:10)
at module.exports (/home/user/Documents/linux/Linux foundation/LFW211/nodeschool/mymodule.js:7:5)
at Object. (/home/user/Documents/linux/Linux foundation/LFW211/nodeschool/make-it-modular.js:5:1)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'ERR_INVALID_ARG_TYPE'
}

OK, nothing strange, the path can't be undefined...

My question is** why this error isn't thrown inside the readdir function ?**

Also, if I add a line

if (dir === undefined) return callback(new Error('Must specify a valid path'))

before the readdir call, the script passes the error but nodeschool validation still returns the same error from above (complaining that mymodule.js isn't passing the error).

Can someone please help me understand what is going on ?

Thanks in advance

Categories

Upcoming Training