Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Require modules first, not inside functions" #1182

Open
avin-kavish opened this issue Jun 17, 2022 · 1 comment
Open

"Require modules first, not inside functions" #1182

avin-kavish opened this issue Jun 17, 2022 · 1 comment

Comments

@avin-kavish
Copy link

avin-kavish commented Jun 17, 2022

"Require modules first, not inside functions"

This causes the module to be loaded optimistically when the scripts are loaded, which increases the boot time of the app. In cases where boot time is important, this is not desirable behaviour. Regarding the point about this catching errors early, having a comprehensive test suite will help catch errors due to missing deps in functions.

@avin-kavish avin-kavish changed the title Require modules first, not inside functions "Require modules first, not inside functions" Jun 17, 2022
@josh-hemphill
Copy link
Collaborator

When implementing async imports in large project, you also run into lots of issues. I've done a lot of web servers where auto-imports resolve with dynamic imports out of necessity, and while it may "start" right away, you have no guarantees about what has loaded and is functional.
Maybe some updates to that section is warranted, but another huge problem with "require" inside functions is if you ever want to convert to ESM syntax, you'll have to convert a lot of stuff to async/await. Personally I also prefer to bundle and pre-optimize my server projects when possible, and lazy-loading or dynamic imports usually either entirely prevent or cripple the ability to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants