Fix #979 module.exports when module is required before exports

Fixes #979

I tried to add some tests, but didn't really figure out how those work. Probably should be refactored first before merging. If you can tell me how I can try to fix the tests.