无法在噩梦回调函数中使用节点模块

发布于 2025-01-11 16:24:44 字数 1649 浏览 0 评论 0原文

我试图在噩梦评估回调函数中使用节点模块,但每次运行代码时都会收到错误,提示 html2json is not Defined。我尝试使用 import 和 require 来导入我的模块,但它不起作用。我还尝试使用闭包来传递模块并返回我使用该特定模块的回调函数,但它也不起作用。如果我在回调函数之外使用这个模块,它工作得很好。非常感谢对此的任何帮助。

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();

我每次都会遇到的错误:

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-01-18 16:24:44

您在评估()中传递的函数在浏览器中运行。就像通过在网络浏览器中打开网页来运行该功能一样。因此,它无权访问节点模块。

方法是选择并提取您感兴趣的元素的属性,然后在 then() 中对其进行处理。
then() 将有权访问节点模块。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文