Node-Fetch needs import on debug but does not work on production

I am using, node-fetch to query my devices on the device.js
On debug, I need to import
const fetch = require('node-fetch');
Without it, I get this error when running homey app run:

[ManagerDrivers] [Driver:solarpanel] [Device:09843584-dbce-45d8-bd3d-22068247553a] Error:  ReferenceError: fetch is not defined
    at SolarPanel.pullData (/app/drivers/solarpanel/device.js:26:7)

However, leaving the line in on production (deploying via homey app install), breaks the app. I get this error both in the developer tools and homeyscript:

Could not get device by ID.

Leaving that line out works fine in production for some reason.

What does “on debug” mean? Do you mean homey app run or homey app run --remote? (and if so, which one specifically?) Which Homey model?

Okay, I see the issue:

  • HP2023, homey app run --remote: fetch is defined (probably built into Node)
  • HP2023, homey app run: fetch is not defined
  • Older HP’s: fetch is not defined

So if you really want to use fetch, the best way is to ship your app with node-fetch as a dependency.

I think the error (“Could not get device by ID”) is unrelated.

1 Like

So if you really want to use fetch ,
Do you have an alternative in mind?

For Homeyscript, all you have available is fetch, for apps you can use something like http.min, but it depends on your specific requirements.

1 Like