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

82.1k views Asked by At

I have an express server written in typescript with "module": "es2020" in its tsconfig.

I've also developed another es2020 module for my graphql API, still in typescript, and this module uses mongoose with such named imports:

import { Types } from 'mongoose'

Everything works fine when I compile my graphql module with tsc. But the express server which is ran with

nodemon --watch './**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' src/index.ts

is unable to handle the mongoose named import.

import { Types } from 'mongoose';
         ^^^^^
SyntaxError: Named export 'Types' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'mongoose';
const { Types } = pkg;

Solution #1

import mongoose from 'mongoose'

And replace Types by mongoose.Types.

But since tsc can handle the mongoose named import, I have hope that it's also possible to make ts-node able to do so.

Solution #2

Switch to commonjs, I can keep the import/export syntax in my graphql module and compile it as a cjs module. But I would have to use a cjs syntax in my express server, and I don't want to.

2

There are 2 answers

2
Evans Atom On

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

0
ZuzEL On

If you encounter this error having a mono-repo, make sure the module that you have problems with, has

"type": "module" 

in package.json.

It seems that Nodejs will consider packages to be commonjs by default and you need to explicitly tell it that your package is a module.