【问题标题】:cheerio is not a function on the server with same node.js version as locallyCheerio 不是服务器上与本地具有相同 node.js 版本的功能
【发布时间】:2021-06-09 13:12:39
【问题描述】:

我有一个使用 Cheerio 的刮刀。

var cheerio = require('cheerio');
$ = cheerio.load(s, { decodeEntities: true });
var sel = $('.text');
cheerio(sel).find(selector)

当我在本地调用它时,它工作正常。当我在具有 Cent OS 但在本地使用相同节点版本 (12.18.3) 的服务器上调用它时,我收到 cheerio is not a function 错误。我清除了本地和远程的 node_modules 文件夹并创建了npm install,但没有更改。还有什么可能导致这种奇怪的行为?

【问题讨论】:

    标签: javascript node.js cheerio


    【解决方案1】:

    我发现有时require('packageName').default 需要导出。所以我试了一下,可以在远程修复它,但在本地 require('cheerio').default 不起作用。现在它可以在本地和服务器上使用以下解决方案:

    var cheerio = require('cheerio');
    if (typeof (cheerio) != 'function') cheerio = require('cheerio').default;
    

    我只是不知道为什么在具有相同 node.js 版本的两台机器上它会有所不同。

    【讨论】:

    • 不同的cheerio版本?
    • 不,它是同一个 Cheerio 版本 1.0.0-rc.5
    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    相关资源
    最近更新 更多