Skip to content

Instantly share code, notes, and snippets.

@wmantly
Created December 31, 2023 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmantly/128997ebfd294a0701d05a46782e61f2 to your computer and use it in GitHub Desktop.
Save wmantly/128997ebfd294a0701d05a46782e61f2 to your computer and use it in GitHub Desktop.
Get rid of `../` from `require()`
const Module = require('module');
const original_resolveFilename = Module._resolveFilename;
Module._resolveFilename = function(...args){
args[0] = args[0].startsWith('>') ? args[0].replace('>', __dirname) : args[0];
return original_resolveFilename(...args);
};

I have grown tired of having lots of ../ in my require paths... so i took matters into my own hands!

Lets say you have a require like this one require('../../../conf'), if you move the file, it will break.

The following JS code addes > as a short cut to where app.js(or what ever the entry file is) so now you can get a clean

require that looks like this require('>/conf') and is valid anywhere in your project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment