Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@tgwf/co2

thegreenwebfoundation38.5kApache-2.00.16.8TypeScript support: definitely-typed

Work out the co2 of your digital services

sustainability, carbon, the, green, web, foundation, co2

readme

CO2.js

All Contributors

One day, the internet will be powered by renewable energy. Until that day comes, there’ll be a CO2 cost that comes with every byte of data that’s uploaded or downloaded. By being able to calculate these emissions, developers can be empowered to create more efficient, lower carbon apps, websites, and software.

Documentation | Changelog | Roadmap

What is CO2.js?

CO2.js is a JavaScript library that enables developers a way to estimate the emissions related to use of their apps, websites, and software.

Why use it?

Being able to estimate the CO2 emissions associated with digital activities can be of benefit to both developers and users.

Internally, you may want to use this library to create a carbon budget for your site or app. It is also useful for inclusion in dashboards and monitoring tools.

For user facing applications, CO2.js could be used to check & block the uploading of carbon intensive files. Or, to present users with information about the carbon impact of their online activities (such as browsing a website).

The above are just a few examples of the many and varied ways CO2.js can be applied to provide carbon estimates for data transfer. If you’re using CO2.js in production we’d love to hear how! Contact us via our website.

Installation

Using NPM

You can install CO2.js as a dependency for your projects using NPM.

npm install @tgwf/co2

Using esm.sh

You can import the CO2.js library into projects using esm.sh.

import tgwf from "https://esm.sh/@tgwf/co2@latest";

Using a JS CDN

You can get the latest version of CO2.js using one of the content delivery networks below.

jsDelivr

You can find the package at https://www.jsdelivr.com/package/npm/@tgwf/co2.

  • CommonJS compatible build https://cdn.jsdelivr.net/npm/@tgwf/co2@latest/dist/cjs/index-node.min.js
  • ES Modules compatible build https://cdn.jsdelivr.net/npm/@tgwf/co2@latest/dist/esm/index.js
  • IIFE compatible build https://cdn.jsdelivr.net/npm/@tgwf/co2@latest/dist/iife/index.js

Unpkgd

You can find the package at https://unpkg.com/browse/@tgwf/co2@latest/.

  • CommonJS compatible build https://unpkg.com/@tgwf/co2@latest/dist/cjs/index-node.min.js
  • ES Modules compatible build https://unpkg.com/@tgwf/co2@latest/dist/esm/index.js
  • IIFE compatible build https://unpkg.com/@tgwf/co2@latest/dist/iife/index.js

Build it yourself

You can also build the CO2.js library from the source code. To do this:

  1. Go to the CO2.js repository on GitHub.
  2. Clone or fork the repository.
  3. Navigate to the folder on your machine and run npm run build in your terminal.
  4. Once the build has finished running, you will find a /dist folder has been created. Inside you can find:

    • dist/cjs - A CommonJS compatible build.
    • dist/esm - An ES Modules compatible build.
    • dist/iife - An Immediately Invoked Function Expression (IIFE) version of the library.

TypeScript support

Type definitions for CO2.js are published in the DefinitelyTyped project, and are available on NPM at @types/tgwf__co2.

If you want to use type definitions in your project, they should be installed as a devDependency.

npm install --dev @types/tgwf__co2

Marginal and average emissions intensity data

CO2.js includes yearly average grid intensity data from Ember, as well as marginal intensity data from the UNFCCC (United Nations Framework Convention on Climate Change). You can find the data in JSON and CommonJS Module format in the data/output folder.

Using emissions intensity data

You can import annual, country-level marginal or average grid intensity data into your projects directly from CO2.js. For example, if we wanted to use the average grid intensity for Australia in our project, we could use the code below:

import { averageIntensity } from "@tgwf/co2";
const { data } = averageIntensity;
const { AUS } = data;
console.log({ AUS });

All countries are represented by their respective Alpha-3 ISO country code.

Publishing to NPM

We use np to publish new versions of this library to NPM. To do this:

  1. First login to NPM by running the npm login command in your terminal.
  2. Then run npx np <VERSION>.
  3. np will run several automated steps to publish the new package to NPM.
  4. If everything runs successfully, you can then add release notes to GitHub for the newly published package.

Release communication

CO2.js releases will be communicated through the following channels:

Channel Minor Release (0.xx) Patch Release (0.xx.x)
Github
Green Web Foundation website
W3C Slack Sustainability Channel
ClimateAction.Tech Slack
Green Web Foundation LinkedIn Account

Licenses

The code for CO2.js is licensed Apache 2.0. (What does this mean?)

The average carbon intensity data from Ember is published under the Creative Commons ShareAlike Attribution Licence (CC BY-SA 4.0). (What does this mean?)

The marginal intensity data is published by the Green Web Foundation, under the Creative Commons ShareAlike Attribution Licence (CC BY-SA 4.0). (What does this mean?)

See LICENCE for more.

Contributors

To contribute changes back to this project, please follow the steps outlined in the CONTRIBUTING.md file in this repository.

Chris Adams
Chris Adams

💻
fershad
fershad

💻 📖 🚧
Peter Hedenskog
Peter Hedenskog

💻
Dryden
Dryden

💻
Evan Hahn
Evan Hahn

💻 ⚠️
Prathum Pandey
Prathum Pandey

🐛 💻
Frazer Smith
Frazer Smith

💻 ⚠️
Hamish Fagg
Hamish Fagg

💻
Atul Varma
Atul Varma

💻
Piper
Piper

💻
Raymundo Vásquez Ruiz
Raymundo Vásquez Ruiz

💻
JamieB
JamieB

💻
p-gerard
p-gerard

🐛 💻
Simon Fishel
Simon Fishel

💻
Dani Subject
Dani Subject

💻
Malay Kumar
Malay Kumar

📖
Samuel Ikoli
Samuel Ikoli

📖
Ash Johns
Ash Johns

🐛 💻
Matthew Griffin
Matthew Griffin

📖 💻
Hannah
Hannah

📖
Alex
Alex

💻

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

  • Added for new features.
  • Changed for changes in existing functionality.
  • Deprecated for soon-to-be removed features.
  • Removed for now removed features.
  • Fixed for any bug fixes.
  • Security in case of vulnerabilities.

Unreleased

  • Add ability to create adaptors which provide access to external data sources #134
  • Add access to the Electricity Maps API free tier #134
  • Allow access to individual models and their internal functions #195

Released

[0.16.7] - 2025-04-18

Changed

  • Automated monthly update of annual average grid intensity data.
  • Bump esbuild from 0.14.54 to 0.25.0

[0.16.6] - 2025-02-10

Changed

  • Automated monthly update of annual average grid intensity data.

[0.16.5] - 2025-01-20

Changed

  • Automated monthly update of annual average grid intensity data.

[0.16.4] - 2024-12-10

Changed

  • Separate options parsing for byteTrace and visitTrace functions in #226
  • Automated monthly update of annual average grid intensity data.

[0.16.3] - 2024-11-30

Fixed

  • Include all gridIntensity values when running the perVisitTrace or perByteTrace functions. #237

[0.16.2] - 2024-11-12

Added

  • Add JSDoc comments to data generation scripts and outputs #230
  • Added basic error handling to data fetch in emissions script #221

Fixed

  • Bump url2green version #238

Changed

  • Automated monthly update of annual average grid intensity data.

[0.16.1] - 2024-09-10

Changed

  • [Experimental] Add minimum capability to publish CO2.js to JSR.
  • Automated monthly update of annual average grid intensity data.

[0.16.0] - 2024-07-01

Added

  • Add the Sustainable Web Design Rating system #205
  • Add the updated Sustainable Web Design Model version 4 #119

Changed

  • Automated monthly update of annual average grid intensity data.

[0.15.0] - 2024-05-03

Changed

  • Add access to more verbose data response from Greencheck API

Removed

  • Removed Page X-ray specific functions:
    • perDomain
    • perPage
    • perContentType
    • dirtiestResources
    • perParty

[0.14.4] - 2024-03-08

Changed

  • Automated monthly update of annual average grid intensity data.

Deprecated

  • Removing Page X-ray specific functions:
    • perDomain
    • perPage
    • perContentType
    • dirtiestResources
    • perParty

[0.14.3] - 2024-02-21

Changed

  • Automated monthly update of annual average grid intensity data.

[0.14.2] - 2024-01-29

  • Adds user agent header for requests to Green Web Foundation APIs

[0.14.1] - 2024-01-09

Changed

  • Removed index.d.ts in favour of importing type definitions from @types/tgwf__co2.
  • Reduce package size by excluding files from publish NPM package.
  • Automated monthly update of annual average grid intensity data.

[0.13.10] - 2023-12-16

Changed

  • Automated monthly update of annual average grid intensity data.

[0.13.9] - 2023-11-07

Fixed

  • Fix to return expected results when variables with 0 value are passed into function

Changed

  • Automated monthly update of annual average grid intensity data.

[0.13.8] - 2023-10-09

Fixed

  • Properly use value of 0 for system segments and variables in the perByteTrace and perVisitTrace functions.

Changed

  • Automated monthly update of annual average grid intensity data.

[0.13.7] - 2023-09-13

Changed

  • Automated monthly update of annual average grid intensity data.

[0.13.6] - 2023-08-08

Changed

  • Automated monthly update of annual average grid intensity data.
  • Create FUNDING.yml to allow sponsor contribution to this project.
  • Store segment flag on CO2 instance, not models.

[0.13.5] - 2023-07-5

Changed

  • Automated monthly update of annual average grid intensity data.

[0.13.4] - 2023-05-24

Fixed

  • Fixed an error when try to use global grid intensities in IIFE. (PR #147)

[0.13.3] - 2023-05-18

Changed

  • Updated the global grid intensity constant to use the latest WORLD grid intensity value from Ember. (PR #142)

[0.13.2] - 2023-04-21

  • Fix to ensure that region names that are keys in the average annual grid intensity export are capitalised.

[0.13.1] - 2023-04-20

Fixed

  • Fixed the import of average grid intensities in node. (PR #137)

[0.13.0] - 2023-04-13

Changed

In PR #135 there were significant changes made to how average annual grid intensities are fetched and generated.

  • Updated average annual grid intensities to include 2022 values from Ember.
  • Changed the functions to generate average grid intensities to:
    • Fetch data directly from Ember's API.
    • Get the latest annual average values for each country/region.
  • Renamed the average grid intensities export file.

[0.12.2] - 2023-03-01

Added

  • Add a module declaration for use from typescript (PR #131)

Changed

  • Updated 2021 average annual grid intensity values (PR #133)

[0.12.1] - 2023-02-02

Fixed

  • Removed incorrectly imported constants in tests.

[0.12.0] - 2023-02-02

Added

  • Introduced two new functions perByteTrace and perVisitTrace which allow developers to pass an options object containing customised values for the constants used in the Sustainable Web Design model. (PR #126)

Changed

  • Allowed developers now have the option to return a breakdown of carbon emissions estimates by system segment when using the Sustainable Web Design model. (PR #113)

[0.11.4] - 2022-12-02

Fixed

  • Updated the greenCheckMulti function to work properly in ESM. (PR #123)

[0.11.3] - 2022-10-13

Fixed

  • Corrected the Node export for the hosting raised in issue #110. (PR #118)

[0.11.2] - 2022-10-11

Fixed

  • v0.11.x updates increased library size to 17MB + when published to NPM. This has been raised in #108 and it was found data files were being included in the published package. (PR #117)

[0.11.1] - 2022-10-07

Changed

  • We have used generic filenames for data files, to avoid any confusion around the data being provided in this library. (PR #112)

[0.11.0] - 2022-10-03

Added

  • Introduced average and marginal carbon intensity data into the library. This comes from Ember Climate (for average carbon intensity data), and The Green Web Foundation (marginal intensity data, originally sourced from the UNFCCC - the United Nations Framework Convention on Climate Change). For more, see our release guide for v0.11 about the differences between the kinds of data. See #64, and #97 for more.
  • Added new paths to import and require the annual, country-level average and marginal carbon intensity data mentioned above like, as javascript objects, or as JSON. See #104 for more.
  • Added links to CO2.js in forms already available on CDNs to avoid needing to npm install it to try it out. See #105 for more.
  • Introduced scripts to automate the generation of grid intensity data based of Ember & UNFCCC source files.
  • Introduced a release:minor command, to automate the publishing process, to complement release:patch.

Changed

  • Changed the default model for transfer based CO2 calculations from the 1byte model to the Sustainable Web Design model instead. See https://developers.thegreenwebfoundation.org/co2js/models/ for guidance on the differences and how to migrate between them. See #94 for more.
  • Updated our release commands to generate and format the carbon intensity data as part of the release process.

[0.10.4] - 2022-08-12

Added

  • Introduced a release:patch command, to automate the publishing process. This is designed to make sure we always publish the most recent compiled code, by adding a rebuild step that can be easy to forget.

[0.10.3] - 2022-08-12

Added

[0.10.2] - 2022-08-12

  • Added the ability to set the model used by CO2.js to the Sustainable Web Design model, using a simple 'swd' string, instead of needing to pass in a class.

[0.10.1] - 2022-08-01

This release used a version bump as previously we had released v0.10.0 under a pre-release tag.

[0.10.0] - 2022-06-27

  • Added ES import syntax as the main way for handling imports and exports of code within the module.
  • Changed eslint settings to use later version of ecmascript (2020)
  • Change the build tools to use esbulid with jest instead of babel
  • Added more consistent use of the debug logging library in files using the updated import syntax
  • Fixed the incorrect order of FIRST_TIME_VIEWING_PERCENTAGE and RETURNING_VISITOR_PERCENTAGE constants in the SWD model. This will result in larger values for calculations using the sustainable web design, and the default caching assumptions.

[0.9.0] - 2022-03-28

Added

  • Added newly implemented Sustainable Web Design model [thanks @dryden!]
  • Added new readme page for using both emissions models
  • Added new source of data to the Sustainable Web Design model from Ember Climate.

Changed

  • Changed the CO2 class to accept either the One Byte model or the Sustainable Web Design model

Fixed

  • Fixed various typos.

[0.8.0] - 2021-11-28

Fixed

  • Update further dependencies
  • Fix embarassing order of magnitude typo in 1byte model (thanks @mstaschik!)

Added

  • Read JSON blob also as gzipped #44 (thanks @soulgalore)

Changed

  • The 1byte model will give different numbers now. It's mentioned in #fixed but it's worth repeating.

[0.7.0] - 2021-11-28

Fixed

  • Update tests to avoid network requests #50
  • Update dependencies across the board

Changed

  • Switch to github actions instead of travis for CI.

[0.6.1] - 2020-03-15

Fixed

  • Added the function to load JSON, on the tgwg.hosting object, for use in the sustaiable web sitespeed plugin.

[0.6.0] - 2020-03-15

Added

  • Added the hosting-JSON for running local checks against an array instead of SQLite.

Changed

  • Swapped out checking against a sqlite database hosting-jsonin favour of simple array in,
  • Updated conventions for style - using kebab-cases over CamelCase for naming files

Removed

  • Extracted sqlite usage and dependencies into a separate module, url2green. This means you no longer need to compile SQLite on install.

[0.5.0] - 2020-03-03

Changed

  • Updated README
  • Updated the emissions figured for green energy after further research on methodology with @@JamieBeevor
  • Incorporated class based CO2 models from @soulgalore
  • Credit contributors

[0.4.7] - 2020-03-02

Added

  • Added a changelog at last!

[0.4.6] - 2020-03-01

Added

  • Changelog inconsistency section in Bad Practices

[0.4.4] - 2020-03-01

Added

Added the (currently unused) green byte model.

Changed

Update the 1byte model to use an average of devices, rather than just wifi

[0.4.3] - 2020-03-01

Added

Changed

Split hosting API into two separate files (one for sqlite, and one relying on the greencheck API)