Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The description of how require() works is not quite correct. Node does not always fully initialize the dependency module before returning from require(). It can't work that way, because Node allows circular require() dependencies. Try it!


The reason is not correct. Nodejs cache the require lot and so circular dependencies can work without returning half finished loads.


Nodejs does cache the results of require(), but that doesn't help with this problem. Example: https://repl.it/repls/SwiftOlivedrabBrace


> Node does not always fully initialize the dependency module before returning from require().

Unsure what this means. The only reason this happens is because an unfinished copy is provided to the dependency that is causing a circular reference.


It means you can get a half-initialized module back from a require() call.


Does that mean the required modules are not run?


They are run, but if there are circular dependencies they are run _after_ require() returns.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: