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

284
Visualizações
npm import from same index.js that the current file is exported from

Suppose I have created an npm module named @myscope/blurfl that contains a couple of classes: A, defined in A.js and B defined in B.js, that are both re-exported through blurfl/index.js:

@myscope/
    blurfl/
        A.js
        B.js
        index.js

A.js:

export class A {...}

B.js:

import { A } from './A.js';
export class B {...}

index.js:

export * from "./A.js";
export * from "./B.js";

I would prefer to use import { A } from '@myscope/blurfl' instead of import {A} from './A.js' to keep the code cleaner (and make it easier to move an export into a different file), but @myscope/blurfl is obviously not a dependency of @myscope/blurfl itself, so the node module resolver can't find it if I run node index.js to check for missing dependencies.

Is there any way to import another item co-exported from the same index.js file without using the item's explicit filename?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm assuming you are using a current version of Node.js (12 LTS or later). Make sure that the "name" of your package in package.json is really "@myscope/blurfl" (and not just "blurfl"). Now, add an "exports" section to the package.json specifying the relative path of the main export, i.e.

"exports": {
    ".": "./index.js"
}

You should now be able to use import { A } from '@myscope/blurfl' from within your package, too.

So, to summarize:

  • Use Node 12 or later.
  • Check the "name" in package.json.
  • Add an "exports" section to package.json indicating the main entry point export.
about 4 years ago · Juan Pablo Isaza 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