WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Expert · question 70 of 100

How do you implement a custom "require" function to load modules dynamically in a Node.js application?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

In Node.js, the require() function is used to load modules that are required for a particular file or project. By default, Node.js uses CommonJS module format, which allows you to define and load modules with a specific syntax. However, sometimes you may need to load modules dynamically or based on certain conditions. In such cases, you can implement a custom require function that can load modules dynamically.

Here is an example of how to implement a custom require function in Node.js:

    const fs = require('fs');
    const path = require('path');
    
    function customRequire(modulePath) {
        const fullModulePath = path.resolve(__dirname, modulePath);
        
        if (require.cache[fullModulePath]) {
            // If module already exists in cache, return it
            return require.cache[fullModulePath].exports;
        }
        
        // Read module file
        const moduleCode = fs.readFileSync(fullModulePath, 'utf8');
        
        // Create a new module object
        const module = {
            id: fullModulePath,
            exports: {},
            loaded: false,
            require: customRequire,
        };
        
        // Cache the module
        require.cache[fullModulePath] = module;
        
        // Execute module code
        const wrapperFunc = eval(`(function (module, exports, require) { ${moduleCode} })`);
        wrapperFunc(module, module.exports, module.require);
        
        // Mark module as loaded and return exports
        module.loaded = true;
        return module.exports;
    }
    
    // Cache object to store loaded modules
    customRequire.cache = {};
    
    module.exports = customRequire;

In this implementation, we first resolve the absolute path of the module file using path.resolve(). Then we check if the module already exists in the cache. If it does, we return the cached module. Otherwise, we read the module file using fs.readFileSync() and create a new module object with an empty exports object, a loaded flag set to false, and a reference to the custom require function.

We then cache the module in the require.cache object and execute the module code using the eval() function. The wrapperFunc is a self-invoking function that takes the module, exports, and require objects as arguments and executes the module code inside it. Finally, we mark the module as loaded and return the exports object.

Using this custom require function, you can load modules dynamically based on certain conditions or parameters. However, it is important to note that implementing a custom require function can have security implications and should be used with caution.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics