Skip to content

Instantly share code, notes, and snippets.

@virtueer
Last active July 24, 2021 17:16
Show Gist options
  • Save virtueer/584c9d4662e36932697b66f954d85659 to your computer and use it in GitHub Desktop.
Save virtueer/584c9d4662e36932697b66f954d85659 to your computer and use it in GitHub Desktop.
no need to use module-alias, At calling require, it searches directly from the root folder if there is an '@' at the beginning.
const BuiltinModule = require('module');
const path = require('path');
// Guard against poorly mocked module constructors
const Module = module.constructor.length > 1 ? module.constructor : BuiltinModule;
const realResolveFilename = Module._resolveFilename;
Module._resolveFilename = (request, ...args) => {
if (request[0] !== '@') {
return realResolveFilename.call(this, request, ...args);
}
const _path = path.join(process.cwd(), request.slice(1));
return realResolveFilename.call(this, _path, ...args);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment