Named export ‘Types’ not found. The requested module ‘mongoose’ is a CommonJS module, which may not support all module.exports as named exports

TL;DR

All you have to do is remove the curly braces from the Types. That should work. Just like this:

import Types from 'mongoose'

But the name does not matter.

Explanation

import Types from 'mongoose' works because we are importing the package’s default export (this is why the name we use does not matter).

However, when you do import * as Types from 'mongoose you tell JS that you seriously want everything as it is raw. That means that instead of getting the default export:

{
    "function1": {},
    "function2": {}
}

You get this:

{
    "default": {
        "function1": {},
        "function2": {}
    }
}

So you could have also done Types.default but that’s probably not as clean. 🙂

This StackOverflow post suggests that we could make both work, but also suggests it would be a hacky workaround that probably should not work 😓

Leave a Comment