Skip to content

Instantly share code, notes, and snippets.

@marcosbozzani
Created July 8, 2020 16:05
Show Gist options
  • Select an option

  • Save marcosbozzani/1a45b92119f20a03640f0bf7e68b3c7a to your computer and use it in GitHub Desktop.

Select an option

Save marcosbozzani/1a45b92119f20a03640f0bf7e68b3c7a to your computer and use it in GitHub Desktop.
Node root relative require "~/"
const Path = require("path");
const Module = require("module");
const original = Module.prototype.require;
Module.prototype.require = function() {
if (arguments[0][0] === "~") {
arguments[0] = Path.join(__dirname, arguments[0].slice(1));
}
return original.apply(this, arguments);
};
// jsconfig.json
// {
// "compilerOptions": {
// "module": "commonjs",
// "baseUrl": ".",
// "paths": { "~/*": ["./*"] }
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment