thegreenwebfoundation / co2.js

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services
Other
385 stars 48 forks source link

Release 0.13.3 not working / breaking changes #146

Closed Manuel-Steinberg closed 1 year ago

Manuel-Steinberg commented 1 year ago

Error

Uncaught TypeError: Cannot read properties of undefined (reading 'data')
at index.js:18:48
at index.js:7:68

Which points me to here...

// These carbon intensity figures https://ember-climate.org/data/data-explorer
// - Global carbon intensity for 2022
const GLOBAL_GRID_INTENSITY = averageIntensity.data["WORLD"];❌

That looks like this merge a couple of days ago: #142


Which leads to an error in my JS

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'co2')

Reproduce

HTML code:

<script defer type="text/javascript" src="https://unpkg.com/@tgwf/co2@0.13.3/dist/iife/index.js"></script>

JS code:

const emissions = new co2.co2({model: "swd"});

const bytesSent = webAnalyse.bytes // value is set
const greenHost = webAnalyse.green // value is set
let estimatedCO2 = emissions.perVisit(bytesSent, greenHost).toFixed(3) 

Solution (Work-around)

Switching back to the previous version inside the HTML fixed the issue.

<script defer type="text/javascript" src="https://unpkg.com/@tgwf/co2@0.13.2/dist/iife/index.js"></script>
fershad commented 1 year ago

@Manuel-Steinberg sorry that you've encountered this. I have released a patch version which solves for this in my testing.

v0.13.3 https://gitpod.io/#https://github.com/thegreenwebfoundation/co2.js/tree/v0.13.3

v0.13.4 https://gitpod.io/#https://github.com/thegreenwebfoundation/co2.js/tree/v0.13.4

Run npm install and then npm run gitpod to test it in the browser.

This wasn't picked up by our test suite, as we don't have coverage for running tests on the IIFE version in the browser. I'll have a think about how/if we can solve for that.

Manuel-Steinberg commented 1 year ago

Don't be sorry @fershad 😉

I admit that I'm not a big fan / user of npm and "modern" JS. I do not really know the difference between CommonJS, ES Modules, etc. ...

The reason why I am using the IIEF version is that I could add it via script tag and let's go 😬 quick and easy or call it "old-school" 🤓 But it was working! So I just hope this option will not disappear in future releases 😅

fershad commented 1 year ago

No plans on it disappearing soon :)