discussions icon indicating copy to clipboard operation
discussions copied to clipboard

make it modular

Open k0dard opened this issue 4 years ago • 3 comments

  • node version: v14.17.5
  • npm version: 7.24.2
  • os: linux

Hello,

I can't understand why the code isn't passing. When I manually call to provoke error i.e.

node make-it-modular.js 2 js

I can see that error is being passed from mymodule.js to make-it-modular.js

Any help would be appreciated

Error output

 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)

My Code

mymodule.js

'use strict'
const {readdir} = require('fs')
const path = require('path')

module.exports = function (dir, filter, callback) {
    filter = '.' + filter
    readdir(dir, (err, list) => {
        if (err) return callback(err)
        else {
            const data = list.filter(file => path.extname(file) === filter)
            callback(null, data)
        }
        
    })
}

make-it-modular.js

const mymodule = require('./mymodule')
const dir = process.argv[2];
const filter = process.argv[3];
mymodule(dir, filter, (err, data) =>{
    if (err) console.error('error: ' + err)
    else data.forEach(element => console.log(element));
})

Thanks in advance !

k0dard avatar Oct 05 '21 15:10 k0dard

UPDATE

If I call my script without arguments

node make-it-modular.js

the error indeed isn't passed to main file - I get:

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:8:5)
    at Object.<anonymous> (/home/user/Documents/linux/Linux foundation/LFW211/nodeschool/make-it-modular.js:4: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'
}

I don't understand why this error isn't caught by callback function in readdir ?

k0dard avatar Oct 06 '21 18:10 k0dard

The "problem" was in the line const {readdir} = require('fs')

When I replaced it with const fs = require('fs')

and also changed the call (from readdir(...) to fs.readdir(...) the tests passed...

The mentioned error when script is called without arguments is still there though

k0dard avatar Oct 16 '21 11:10 k0dard

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 08 '22 22:01 stale[bot]