UNPKG

5.13 kBMarkdownView Raw
1# CO2.js
2
3<img src="https://github.com/thegreenwebfoundation/co2.js/actions/workflows/unittests.yml/badge.svg" />
4
5One 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.
6
7## [Documentation](https://developers.thegreenwebfoundation.org/co2js/overview/)
8## [Changelog](/CHANGELOG.md)
9
10## What is CO2.js?
11
12CO2.js is a JavaScript library that enables developers a way to estimate the emissions related to use of their apps, websites, and software.
13
14## Why use it?
15
16Being able to estimate the CO2 emissions associated with digital activities can be of benefit to both developers and users.
17
18Internally, 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.
19
20For 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).
21
22The above a 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](https://www.thegreenwebfoundation.org/support-form/) via our website.
23
24## Installation
25
26### Using NPM
27
28You can install CO2.js as a dependency for your projects using NPM.
29
30```bash
31npm install @tgwf/co2
32```
33
34### Using Skypack
35
36You can import the CO2.js library into projects using Skypack.
37
38```js
39import tgwf from 'https://cdn.skypack.dev/@tgwf/co2';
40```
41
42## Using a JS CDN
43
44You can get the latest version of CO2.js using one of the content delivery networks below.
45
46### jsDelivr
47
48You can find the package at [https://www.jsdelivr.com/package/npm/@tgwf/co2](https://www.jsdelivr.com/package/npm/@tgwf/co2).
49
50- CommonJS compatible build `https://cdn.jsdelivr.net/npm/@tgwf/co2@latest/dist/cjs/index-node.min.js`
51- ES Modules compatible build `https://cdn.jsdelivr.net/npm/@tgwf/co2@latest/dist/esm/index.js`
52- IIFE compatible build `https://cdn.jsdelivr.net/npm/@tgwf/co2@latest/dist/iife/index.js`
53
54### Unpkgd
55
56You can find the package at [https://unpkg.com/browse/@tgwf/co2@latest/](https://unpkg.com/browse/@tgwf/co2@latest/).
57
58- CommonJS compatible build `https://unpkg.com/@tgwf/co2@latest/dist/cjs/index-node.min.js`
59- ES Modules compatible build `https://unpkg.com/@tgwf/co2@latest/dist/esm/index.js`
60- IIFE compatible build `https://unpkg.com/@tgwf/co2@latest/dist/iife/index.js`
61
62### Build it yourself
63
64You can also build the CO2.js library from the source code. To do this:
65
661. Go to the [CO2.js repository](https://github.com/thegreenwebfoundation/co2.js) on GitHub.
671. Clone or fork the repository.
681. Navigate to the folder on your machine and run `npm run build` in your terminal.
691. Once the build has finished running, you will find a `/dist` folder has been created. Inside you can find:
70
71 - `dist/cjs` - A CommonJS compatible build.
72 - `dist/esm` - An ES Modules compatible build.
73 - `dist/iife` - An Immediately Invoked Function Expression (IIFE) version of the library.
74
75## Marginal and average emissions intensity data
76
77CO2.js includes yearly average grid intensity data from [Ember](https://ember-climate.org/data/data-explorer/), as well as marginal intensity data from the [UNFCCC](https://unfccc.int/) (United Nations Framework Convention on Climate Change). You can find the data in JSON and CommonJS Module format in the `data/output` folder.
78
79### Using emissions intensity data
80
81You 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:
82
83```js
84import { averageIntensity } from '@tgwf/co2';
85const { data } = averageIntensity;
86const { AUS } = data;
87console.log({ AUS })
88```
89
90All countries are represented by their respective [Alpha-3 ISO country code](https://www.iso.org/obp/ui/#search).
91## Publishing to NPM
92
93We use [`np`](https://www.npmjs.com/package/np) to publish new versions of this library to NPM. To do this:
94
951. First login to NPM by running the `npm login` command in your terminal.
962. Then run `npx np <VERSION>`.
973. `np` will run several automated steps to publish the new package to NPM.
984. If everything runs successfully, you can then add release notes to GitHub for the newly published package.
99
100
101## Licenses
102
103The code for CO2.js is licensed Apache 2.0.
104
105The average carbon intensity data from Ember is published under the Creative Commons ShareAlike Attribution Licence ([CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/))
106
107The marginal intensity data is published by the Green Web Foundation, under the Creative Commons ShareAlike Attribution Licence ([CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)).
108
109See LICENCE for more.