Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

2.3K
Vistas
'Directory import is not supported resolving ES modules' with Node.js

I'm using Node.js v14.13.0.

app.js file:

import database from './database';

database();

database/index.js file:

import mongoose from 'mongoose';

export default connect = async () => {
    try {
        await mongoose.connect('...', { });
    } catch (error) {}
};

In package.json I added "type": "module".

After running the app I get the following error:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/xx/Desktop/Projects/node-starter/src/database' is not supported resolving ES modules imported from /Users/xx/Desktop/Projects/node-starter/src/app.js

over 4 years ago · Santiago Trujillo
5 Respuestas
Responde la pregunta

0

TValidator's answer is somewhat right, you can use babel to resolve this issue.
However, you must install @babel/core, @babel/node, @babel/preset-env.
And, you must make babel.config.json like belows;

{
    "presets": ["@babel/preset-env"]
}

You should stay babel.config.json in the root folder of your project.
Now, you don't have to set "type": "module" to the package.json.

over 4 years ago · Santiago Trujillo Denunciar

0

What happens here is that Node mandates using an extension in import statements and also states,

Directory indexes (e.g. './startup/index.js') must also be fully specified.

Your import database from './database'; statement doesn't specify the index.js. You can add index.js as suggested in another answer, but that approach doesn't look elegant in TypeScript projects, when you'd end up importing a .js file from a .ts one.

You can change this Node extension resolution behavior by passing the --experimental-specifier-resolution=node flag. This will work and will keep your code unchanged:

app.js

import database from './database';
database();

Run as: node --experimental-specifier-resolution=node app.js.

A Node developer admitted that the documentation wasn't that clear.

over 4 years ago · Santiago Trujillo Denunciar

0

With ES6 modules you can not (yet?) import directories. Your import should look like this:

import database from "./database/index.js"
over 4 years ago · Santiago Trujillo Denunciar

0

According to nodejs documentation, directory imports doesn't work

over 4 years ago · Santiago Trujillo Denunciar

0

You can use babel. I used following babel library in node project and every ES6 features work properly.

"devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-env": "^1.6.1"
  }

Although it may be old version but you can use latest version.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda