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

110
Visualizações
Global object in node.js

I have in my website a class named classPerson which is in dir /foo. I need to use it in dir ./bar, but since /bar is not allowed to import from /foo i do the following:

/foo:

  const classPerson = new classPerson();

  Object.assign(window, { classPerson });

/bar:

const person = window.classPerson.

I need to achieve a similar thing in node.js. How can i do it (having no window)

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Depending on your project setup, you can use either the built-in require() method or the es6 import module syntax.

in /foo.js

export class ClassPerson { ... } // add the export keyword to the class definition

export const classPersonInstance = new classPerson(); // add the export keyword to the class instance if you want to use the same instance

in /bar.js

const foo = require('./foo.js');
const instance = foo.classPersonInstance;

// or
import { classPersonInstance } from './foo.js';

NOTE To be able to use the import syntax, you should do some additional configuration

Here is the nodejs documentation for ECMAScript Modules

about 4 years ago · Santiago Gelvez 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