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

131
Visualizações
Unable to export a function from a custom node.js module

I am testing out a simple node.js custom module represetned by the processTest.js file in the node.js root directory. Here is its contents: notice that the module.exports includes the one function processTest():

'use strict';

const { spawn } = require( 'child_process' );
const ls = spawn( 'ls', [ '-lh', '/usr' ] );

function runTest() {
  ls.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
  });

  ls.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
  });

  ls.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
  });
}
module.exports = runTest;

In the main node.js script we try to invoke it as follows:

const processTest = require("./processTest")

...

http.createServer(app).listen(port, '0.0.0.0', function () {
  console.debug(`HTTP: Listening on ${port} ..`)
  processTest.runTest();
})

However we get the error

Uncaught TypeError: processTest.runTest is not a function

note that the processTest module is actually present and the function is visible.

enter image description here What is wrong here and how should it be corrected?

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

0

You are referring to the function in the processTest file. (module.exports = runTest). Use require("./processTest")()

Or just change the module.exports to

module.exports = {
    runTest
}

and require("./processTest").runTest() will work

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