Skip to content

Instantly share code, notes, and snippets.

@christianscott
Created August 17, 2021 04:28
Show Gist options
  • Save christianscott/0d9641ec4a05468ae3b960b5d53322c2 to your computer and use it in GitHub Desktop.
Save christianscott/0d9641ec4a05468ae3b960b5d53322c2 to your computer and use it in GitHub Desktop.
error on import of 3rd party module
const mod = require('module');
const os = require('os');
const colors = {
red: '\033[0;31m',
none: '\033[0m',
};
const logErr = (msg) => process.stderr.write(`${colors.red}ERROR: ${msg}${os.EOL}${colors.none}`);
mod.prototype.require = new Proxy(mod.prototype.require, {
apply(target, self, args){
const [name] = args;
if (!mod.builtinModules.includes(name)){
logErr(
'cannot require a third-party module from inside this script. ' +
'it is run before `yarn install` so can only depend on builtin modules',
);
process.exit(1);
}
return Reflect.apply(target, self, args);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment