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

163
Vistas
Node package.json Export VS Imports fields

So after a bit of digging around the diferences from Export and Imports declared from the package.json file I was wondering what is the best use case for both?

For example the following fields:

"name": "node-api",
"exports": {
  ".": "./application.js",
  "./config/*": "./config/*.js",
  "./controllers": "./controllers/index.js",
  "./helpers/*": "./helpers/*.js",
  "./models": "./models/index.js",
  "./routes": "./routes/index.js"
},
"imports": {
  "#config/*": "./config/*.js",
  "#controllers": "./controllers/index.js",
  "#helpers/*": "./helpers/*.js",
  "#models": "./models/index.js",
  "#routes": "./routes/index.js"
}

And then each of the following with their output in the main JS file:

import routes from './routes/index.js'; // works
import routes from './routes'; // error - ERR_UNSUPPORTED_DIR_IMPORT
import routes from 'node-api/routes'; // works (with the package name)
import routes from '#routes'; // works (without the package name but need the #)

So why not just use the imports field?

In my opinion seems friendlier than to type your package name every time you want to import your own file.

Based on the NODE JS official docs (https://nodejs.org/api/packages.html) it says the following: "The "exports" field allows defining the entry points of a package when imported by name loaded either via a node_modules lookup or a self-reference to its own name.".

Then for the imports field says the following: "it is possible to define internal package import maps that only apply to import specifiers from within the package itself."

From my testing to reference my relative (my own created) files I just use the imports field so that I don't need to type in the package for every import that I want.

So long story short, when is it best to use exports and imports field and in my case does it make sense to use only imports?

about 4 years ago · Juan Pablo Isaza
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