require.main
is not set on the REPL. If you take a look at it from a Node.js script, it looks like this:
{ id: '.',
exports: {},
parent: null,
filename: '/Users/btilley/test.js',
loaded: false,
children: [],
paths:
[ '/Users/btilley/node_modules',
'/Users/node_modules',
'/node_modules' ] }
I'm guessing npm uses the filename
attribute (and probably others) to resolve paths and the like. You could probably fake it out during a REPL session by setting all the right attributes; you might also take a look at the npm CLI source to see how sets up and/or uses this data.