aseemk / requireDir

Node.js helper to require() directories.
MIT License
484 stars 60 forks source link

module.parent.filename can be undefined #52

Closed nikitaeverywhere closed 6 years ago

nikitaeverywhere commented 6 years ago

Hello!

I think the issue I experienced today is kind of a rare one, but it may happen to anyone else as it did for me. I am not sure what exactly causes module.parent.filename to be undefined, but when I required require-dir from the Intellij IDEA command line, it gave me the following (I am using cmder):

Z:\ZitRos\Projects\TimingKit (master) (zitronic-timing-kit@0.34.7)
λ node --version
v8.0.0

Z:\ZitRos\Projects\TimingKit (master) (zitronic-timing-kit@0.34.7)
λ node
> const requireDir = require("require-dir");
TypeError: Path must be a string. Received null
    at assertPath (path.js:28:11)
    at Object.dirname (path.js:716:5)
    at Object.<anonymous> (Z:\ZitRos\Projects\TimingKit\node_modules\require-dir\index.js:12:22)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)

The change I propose fixes this problem: it just defaults filename to an empty string, which works as expected:

> let requireDir = require("require-dir");
undefined
> Object.keys(requireDir("."))
[ 'package-lock', 'package' ]

Thanks!

stephenlacy commented 6 years ago

This is on windows, right? any node version? This sounds like a bug with node, and should be addressed even if it is patched here.

nikitaeverywhere commented 6 years ago

@stevelacy, thanks for the hint!

This is on windows, right? any node version?

Windows 10.0.15063 / NodeJS 8.2.1 (latest), the same:

λ node -v
v8.2.1

Z:\ZitRos\Projects\TimingKit (master) (zitronic-timing-kit@0.35.5)
λ node
> const requireDir = require("require-dir")
TypeError: Path must be a string. Received null
    at assertPath (path.js:28:11)
    at Object.dirname (path.js:716:5)
    at Object.<anonymous> (Z:\ZitRos\Projects\TimingKit\node_modules\require-dir\index.js:12:22)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)

Look like this here may not be defined... @stevelacy, have you ever took a look on NodeJS sources? Maybe you know the place to dig deeper? Thank you!

stephenlacy commented 6 years ago

Can you run the tests for node v8.2.1? This may be a windows edge case which is untested, and would need fixing in core.

nikitaeverywhere commented 6 years ago

Can you run the tests for node v8.2.1?

That's not trivial :) I will try tomorrow.

This issue started to mysteriously appear not only in Intellij Window, but across the system... Now I cannot launch my project locally. Strange.

nikitaeverywhere commented 6 years ago

And now I can again (tried npm install --save require-dir@latest). Super strange.

yocontra commented 6 years ago

Hi - new maintainer here. Is this still an issue you're having or was it solved by more recent versions of node?

nikitaeverywhere commented 6 years ago

Hi @contra! I'm not sure; I didn't use this module for a long time. That issue were very strange, appearing only a couple of times but not always. I think if no people give their feedback here that was most likely the problem with my configuration/system somewhere.

yocontra commented 6 years ago

@ZitRos Cool, will close and reopen if somebody else reports it again. Thanks for responding.