Skip to content

Change export of check function to an object#267

Open
dotjay wants to merge 1 commit into
thegreenwebfoundation:mainfrom
dotjay:hosting-check-266
Open

Change export of check function to an object#267
dotjay wants to merge 1 commit into
thegreenwebfoundation:mainfrom
dotjay:hosting-check-266

Conversation

@dotjay
Copy link
Copy Markdown

@dotjay dotjay commented Nov 14, 2025

Triage

Type of change

Please select any of the below items that are appropriate.

This pull request:

  • Adds a new carbon estimation model to CO2.js
  • Adds new functionality to an existing model
  • Fixes a bug with an existing model implementation
  • Add other new functionality to CO2.js
  • Add new data to CO2.js
  • Improves developer experience for contributors
  • Adds contributors to CO2.js
  • Does something not specified above

Related issue/s

Docs changes required

Do any changes made in this pull request require parts of the CO2.js documentation to be updated?

  • Yes
  • No
  • I don't know

Describe the changes made in this pull request

Make check() a public method for the ESM version by wrapping the exported method in an object, making more consistent with the CJS version.

@fershad
Copy link
Copy Markdown
Contributor

fershad commented Nov 27, 2025

Thanks @dotjay

When I test out this code on the following, it throws an error (TypeError: hosting is not a function):

const { hosting } = require("@tgwf/co2");

const options = {
  verbose: false,
  userAgentIdentifier: "myGreenApp",
};
hosting("google.com", options).then((result) => {
  console.log(result);
});

I'm wondering if something is happening in the build step that messes with this.

@dotjay
Copy link
Copy Markdown
Author

dotjay commented Dec 9, 2025

I'll try to explain more clearly…

This CommonJS script works. It calls hosting.check()

#!/usr/bin/env node

const { co2, hosting } = require("@tgwf/co2");

const bytes = 100000;
const url = "https://digitalasitshouldbe.com/";

async function main() {
    const co2Model = new co2({ model: "swd", version: 4, rating: true });
    const co2Data = co2Model.perByte(bytes);

    const hostDomain = new URL(url).hostname;
    const isGreenHosted = await hosting.check(hostDomain, { verbose: true });

    console.log(co2Data);
    console.log(isGreenHosted);
}

main();

If I try to run the same code, but as a module:

#!/usr/bin/env node

import { co2, hosting } from "@tgwf/co2";

const bytes = 100000;
const url = "https://digitalasitshouldbe.com/";

async function main() {
    const co2Model = new co2({ model: "swd", version: 4, rating: true });
    const co2Data = co2Model.perByte(bytes);

    const hostDomain = new URL(url).hostname;
    const isGreenHosted = await hosting.check(hostDomain, { verbose: true });

    console.log(co2Data);
    console.log(isGreenHosted);
}

main();

… Node throws an error: TypeError: hosting.check is not a function

This is why I flagged an issue of consistency with the code example in the tutorial in Issue #266. With the ES Module version of the hosting object, the check() method itself is exported rather than an object containing check. I have to change the line in my module code for it to work:

// Note: hosting.check() isn't there in ESM version; use hosting()
const isGreenHosted = await hosting(hostDomain, { verbose: true });

@fershad
Copy link
Copy Markdown
Contributor

fershad commented Dec 11, 2025

@dotjay if you're using v0.17 you can use:

const { check } = require("@tgwf/co2/hosting");
import { check } from "@tgwf/co2/hosting";

So your code examples would become:

CJS:

#!/usr/bin/env node

- const { co2, hosting } = require("@tgwf/co2");
+ const { co2 } = require("@tgwf/co2");
+ const { check } = require("@tgwf/co2/hosting");

const bytes = 100000;
const url = "https://digitalasitshouldbe.com/";

async function main() {
    const co2Model = new co2({ model: "swd", version: 4, rating: true });
    const co2Data = co2Model.perByte(bytes);

    const hostDomain = new URL(url).hostname;
-    const isGreenHosted = await hosting.check(hostDomain, { verbose: true });
+    const isGreenHosted = await check(hostDomain, { verbose: true });

    console.log(co2Data);
    console.log(isGreenHosted);
}

main();

ESM:

#!/usr/bin/env node

- import { co2, hosting } from "@tgwf/co2";
+ import { co2 } from "@tgwf/co2";
+ import { check } from "@tgwf/co2/hosting";

const bytes = 100000;
const url = "https://digitalasitshouldbe.com/";

async function main() {
    const co2Model = new co2({ model: "swd", version: 4, rating: true });
    const co2Data = co2Model.perByte(bytes);

    const hostDomain = new URL(url).hostname;
-    const isGreenHosted = await hosting.check(hostDomain, { verbose: true });
+    const isGreenHosted = await check(hostDomain, { verbose: true });
 
    console.log(co2Data);
    console.log(isGreenHosted);
}

main();

@dotjay
Copy link
Copy Markdown
Author

dotjay commented Dec 15, 2025

That's okay, except I'd argue that it's not an approach I'd expect as a Node developer, i.e. having to import twice from a package, once from a subordinate class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants