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

167
Visualizações
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 à 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