Change export of check function to an object#267
Conversation
|
Thanks @dotjay When I test out this code on the following, it throws an error ( 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. |
|
I'll try to explain more clearly… This CommonJS script works. It calls If I try to run the same code, but as a module: … Node throws an error: 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 |
|
@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(); |
|
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. |
Triage
Type of change
Please select any of the below items that are appropriate.
This pull request:
Related issue/s
Docs changes required
Do any changes made in this pull request require parts of the CO2.js documentation to be updated?
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.