Javascript ES6 cookieconsent causes errors in Hugo

The following example script using vanilla javascript works well but not in Hugo:

I ran “hugo new site quickstart” and incorporated the above script. My package.json therefore has the line “type”: “module”.

I added a11y-dialog as a npm dependency yet it causes a weird error as if it cannot read the dependency in node_modules
import A11yDialog from ‘a11y-dialog’;
Uncaught TypeError: The specifier “a11y-dialog” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “…/” or “/”.

If I delete the above line and add the cookieconsent js files locally, I get the following:
import * as CookieConsent from ‘./cookieconsent-core-esm.js’;
Loading failed for the module with source “http://localhost:1313/js/cookieconsent-core-esm.js”.

Any ideas? I am completely baffled.
Regards, Alfred

Hugo v0.120.3
Node v20.9.0
Ubuntu (as a WSL on Windows)

Solved! It turned out to be an issue with the imported Javascript version: I had to use the umd version instead of the esm. As I use ES6 only I still do not understand that but at least it works :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.