Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

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

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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 😓

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda