Skip to content

Instantly share code, notes, and snippets.

@thetutlage
Last active February 12, 2025 12:25
Show Gist options
  • Save thetutlage/6928cd500491e19b070cc6d23b931ab6 to your computer and use it in GitHub Desktop.
Save thetutlage/6928cd500491e19b070cc6d23b931ab6 to your computer and use it in GitHub Desktop.
Define custom paths for modules resolutions
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