Last active
February 12, 2025 12:25
-
-
Save thetutlage/6928cd500491e19b070cc6d23b931ab6 to your computer and use it in GitHub Desktop.
Define custom paths for modules resolutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const util = require('util') | |
const BuiltinModule = require('module') | |
const Module = module.constructor.length > 1 | |
? module.constructor | |
: BuiltinModule | |
const options = util.parseArgs({ args: process.argv.slice(2), options: { | |
paths: { | |
type: 'string' | |
} | |
}}) | |
const oldNodeModulePaths = Module._nodeModulePaths | |
Module._nodeModulePaths = function (from) { | |
let paths = oldNodeModulePaths.call(this, from) | |
if (options.values.paths) { | |
const customPaths = Array.isArray(options.values.paths) ? options.values.paths : options.values.paths.split(',') | |
paths = paths.concat(customPaths.map((c) => path.join(process.cwd(), c))) | |
} | |
return paths | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment