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

236
Visualizações
Unable to export module in another file in nodejs

I am trying to create some dummy function in nodejs and trying to export it in another file but when I am running it using node index.js it might some sort of silly mistake but its showing error that:

TypeError: spr.comb is not a function

Below is my code:

spread.js

const comb = () => {
   const arr = [4,5,6];
   const newArr = [1,2,3,...arr];
   console.log(newArr);
}

module.exports = comb;

index.js

const spr =  require('./spread');
spr.comb();  

Someone let me know what I am doing wrong.

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

0

Problem is that you have completely overwritten the module.exports object.

module.exports is an empty object to which you can add the values to export from a module.

In your case, you have re-assigned the module.exports to comb function. So, to call the function, change

spr.comb(); 

to

spr(); 

For spr.comb() to work, you need to export the comb function as shown below:

module.exports.comb = comb;

or

exports.comb = { ... }
about 4 years ago · Juan Pablo Isaza Relatório

0

you can do it like this(in spread.js):

const comb = module.exports.comb = () => {
const arr = [4, 5, 6];
const newArr = [1, 2, 3, ...arr];
console.log(newArr);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Can't use a user defined name here because you have exported comb from your spread file

const spr =  require('./spread'); 
spr.comb();

Use this instead

const comb = require('./spread');

If you want to use custom name you need to modify your spread file like this

module.exports = () => {
   const arr = [4,5,6];
   const newArr = [1,2,3,...arr];
   console.log(newArr);
}

Now you are exporting a function from spread, while importing/ including it in index.js you can provide a custom name to this function.

const {custom name comes here}=  require('./spread');

For Example

const comb = require('./spread');
const myCombFn = require('./spread');
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