haensl / google-analytics

Google Analytics 4 JavaScript abstraction for gtag and measurement protocol.
https://www.npmjs.com/package/@haensl/google-analytics
MIT License
1 stars 1 forks source link

Missing import attribute causes error in node 20 #13

Open hello-1tab opened 8 hours ago

hello-1tab commented 8 hours ago

Node.JS throws an error when i try to import measurement-protocol.js file in my code. TypeError [ERR_IMPORT_ASSERTION_TYPE_FAILED]: Module "***/node_modules/@haensl/google-analytics/measurement-protocol.js" is not of type "json"

For example, the import below will fail in Node.JS v17.5.0 and above. import analytics from '@haensl/google-analytics/measurement-protocol.js';

This is because measurement-protocol.js contains following line and import attributes are required since v17.5.0: import http from '@haensl/http';

I think it should be fixed like this - import http from '@haensl/http' with { type: 'json' }; to support newer Node.JS versions.

haensl commented 6 hours ago

Hi,

latest CI run was on Node v22.5.1 and went green. I also don't experience this error in any other project. What's your bundler setup?

hello-1tab commented 6 hours ago

This is very easy to reproduce with node v20.12.2 for example.

Make index.js file with the following content.

import analytics from '@haensl/google-analytics/measurement-protocol.js';

console.log(analytics);

and start the script node index.js

I receive the same error.

hello-1tab commented 6 hours ago

I've seen the pipeline status before the submission of the issue. Not sure why the tests don't catch this issue correctly. Anyway, switching to Node v22.5.1 gives slightly different error when i try running Node.JS on the code above: TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "***/node_modules/@haensl/http/index.json" needs an import attribute of "type: json"

hello-1tab commented 5 hours ago

Probably jest injects custom loader for json files and that's why CI shows different behavior but i'm not sure about this.