Skip to content

Instantly share code, notes, and snippets.

@deepal
Last active September 25, 2019 15:19
Show Gist options
  • Select an option

  • Save deepal/739ccb68d60a8675f46848b02a3b4c44 to your computer and use it in GitHub Desktop.

Select an option

Save deepal/739ccb68d60a8675f46848b02a3b4c44 to your computer and use it in GitHub Desktop.
const workspacePath = '/some/workspace/path';
const workspaceModulesDir = path.join(workspacePath, 'node_modules');
function installModule(moduleName) {
execSync(`npm install --silent ${moduleName}`, { cwd: workspacePath });
}
function wrapRequire() {
const originalRequire = Module.prototype.require;
Module.prototype.require = function (moduleName) {
// Inject workspace node_modules directory
this.paths.unshift(workspaceModulesDir);
try {
return originalRequire.apply(this, [moduleName]);
} catch (err) {
installModule(moduleInfo);
return originalRequire.apply(this, [
path.join(workspacePath, 'node_modules', moduleName),
]);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment