From d59798d50a9d49d734b64b45a0c50163faa58499 Mon Sep 17 00:00:00 2001 From: Mohamed_Hazem Date: Thu, 9 Apr 2026 17:44:45 +0200 Subject: [PATCH] add example for requiring modules at top --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 065d37585..b0cc40e74 100644 --- a/README.md +++ b/README.md @@ -636,6 +636,25 @@ function doSomething() { **Otherwise:** Requires are run synchronously by Node.js. If they are called from within a function, it may block other requests from being handled at a more critical time. Also, if a required module or any of its dependencies throw an error and crash the server, it is best to find out about it as soon as possible, which might not be the case if that module is required from within a function +### Example + +❌ Bad: + +```js + function getUserById(id) { + const db = require('./db'); // required inside function + return db.findUser(id); +} +``` +✅ good: + +```js + const db = require('./db'); // required at top + function getUserById(id) { + return db.findUser(id); + } +``` +

## ![✔] 3.9 Set an explicit entry point to a module/folder