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

Viewed 473

I have a scraper which uses 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. I cleared the node_modules folder on local and remote and made npm install but no change. What else can cause this strange behaviour?

1 Answers

I found out that sometimes require('packageName').default needs to be exported. So I tried it and could fix it on the remote but locally require('cheerio').default wouldn't work. 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.

Related