Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:19:06+00:00 2026-05-23T16:19:06+00:00

Is it possible to define a custom require method in one module that can

  • 0

Is it possible to define a custom require method in one module that can be called in another module?

For example, in x/x.js

exports.xrequire = function(path) {
   console.log("requiring " + path);
   require(path);
};

In y/hello.js

console.log("Hello, world!");

And then in y/y.js

var xrequire = require("../x/x.js").xrequire;
xrequire("hello.js");

When y.js is run, “Hello World” should be printed.

I know this seems like a bad idea, but I do have a legitimate reason to do it. 😉


The issue with this code is that it tries to load x/hello.js, not y/hello.js — I’d like it to work the same as the standard require.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:19:06+00:00Added an answer on May 23, 2026 at 4:19 pm

    You certainly could do this. It would successfully avoid modifying the global require.paths array which can pose problems at run time, and shouldn’t have an issues with future releases.

    You should be able to simply set up a model that you simple require into your current app. According to your example x.js should contain.

    var relative = 'y/';
    exports.xrequire = function(path) {
       console.log("requiring " + relative + path);
       return require(relative+path);
    };
    

    Then you can successfully use your custom require.

    var xrequire = require('x.js'),
        hello = xrequire('hello.js');
    

    I don’t advise this, but if you wanted to be really sneaky, you could override the require function.

    var require = xrequire;
    

    I would definitely throughly document this inside your module if you plan to require 😀 this functionality, but it certainly does work.

    EDIT:

    Well you could test for the file and fallback to require if it doesn’t exist.

    var relative = 'y/';
    exports.xrequire = function(path) {
       var ret;
       try{
         ret = require(relative+path);
         console.log("requiring " + relative + path);
       }catch(e){
         console.log("requiring " + path);
         ret = require(path);
       }
       return ret;
    };
    

    I think you could try adding process.cwd to the beginning of the require path to force looking in the correct directory first. In all honesty this would confuse most developers. I would advise just defining a namespace for your app and creating a special require function that retrieves only that namespaced apps special functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have noticed that it is possible to define a custom class and then
Is it possible to define a custom filter so that NUnit will only run
I know that it is possible to define custom tags in ASP.NET with User
In VBP its possible to define user defined actions by creating a custom COM
Does anyone know if it possible to define the equivalent of a java custom
Is it possible to define a timestamp column in a MySQL table that will
Is it possible to define a class in C# such that class GenericCollection<T> :
This method that uses method-level generics, that parses the values from a custom POJO,
Is it possible to define custom configuration in config/environments/*.rb or config/environment.rb file. Is yes,
It's possible to define your own custom templates for normal project types, such as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.