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

138
Visualizações
Can't use node modules in nightmare callback function

I'm trying to use a node module in nightmare evaluate callback function but every time I run my code I get error which says html2json is not defined. I tried using both import and require to import my module but It didn't work. I also tried using closure to pass the module and return the callback function where I have use that specific module but it didn't work either. If I use this module outside of callback function it works perfectly fine. Any help on this is greatly appreciated.

import Nightmare from "nightmare";
import html2json from "html2json";

const nightmare = Nightmare({ show: false });

async function startScrap() {
    let result = await nightmare
        .goto("https://duckduckgo.com")
        .evaluate(() => {
            const html = html2json.html2json(
                document.querySelector("#r1-0 a.result__a").innerHTML,
            );
            console.log(html);
            return document.querySelector("#r1-0 a.result__a").href;
        })
        .end()
        .then(function (result) {
            console.log(result);
        });
}

startScrap();

The error that I get everytime :

node:internal/process/promises:265
            triggerUncaughtException(err, true /* fromPromise */);          rogramming\Personal Projects\scrappers\animelist-scrapper\test.js"
            ^

ReferenceError: html2json is not defined
    at fn (<anonymous>:6:26)
    at javascript (<anonymous>:25:21)
    at <anonymous>:40:3
    at EventEmitter.electron.ipcRenderer.on (d:\Programming\Personal Projects\scrappers\animelist-scrapper\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:36:30)                                     s\scrappers\animelist-scrapper\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:36:30)
    at emitMany (events.js:147:13)
    at EventEmitter.emit (events.js:224:7) {
  code: -1
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The function you pass in evaluate() is run in browser. Like, running the function by opening web page in your web browser. So, it does not have access to node modules.

Way to do it, is to select and extract the attributes of the elements you are interested in and then process it in then().
then() will have access to node modules.

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