简体   繁体   English

Cheerio 不是服务器上的 function,其 node.js 版本与本地相同

[英]cheerio is not a function on the server with same node.js version as locally

I have a scraper which uses cheerio.我有一个使用 Cheerio 的刮刀。

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

When I call it locally, it works fine.当我在本地调用它时,它工作正常。 When I call it on a server with Cent OS but with the same node version (12.18.3) I use locally, I get cheerio is not a function error.当我在具有 Cent OS 但与我在本地使用的相同节点版本 (12.18.3) 的服务器上调用它时,我得到cheerio is not a function错误。 I cleared the node_modules folder on local and remote and made npm install but no change.我清除了本地和远程的 node_modules 文件夹,并npm install但没有更改。 What else can cause this strange behaviour?还有什么可能导致这种奇怪的行为?

I found out that sometimes require('packageName').default needs to be exported.我发现有时需要导出require('packageName').default So I tried it and could fix it on the remote but locally require('cheerio').default wouldn't work.所以我试了一下,可以在遥控器上修复它,但在本地require('cheerio').default不起作用。 Now it works locally and on the server with the following solution:现在它使用以下解决方案在本地和服务器上工作:

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

I just don't have any idea why it's different on both machines with the same node.js version.我只是不知道为什么在具有相同 node.js 版本的两台机器上它会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM