Error when import JS code from /assets

Why I’m getting Uncaught SyntaxError: import declarations may only appear at top level of a module on custom.js:1 error in my browser console? Have I miss something from documentation?

On the top of custom.js I’m trying to import funcs and const from cookie this way:
import { setCookie, getCookie, deleteCookie } from "js/utils/cookie";

My files, folders structure:
– assets
*|-- js
**|— custom.js
**|— utils
***|— cookie
****|-- index.js

I’m importing my custom.js in HTML this way:

{{ $js := resources.Get "js/custom.js" | js.Build }}
<script src="{{ $js | absURL }}"></script>

I am unable to reproduce the problem.

Replace this

<script src="{{ $js | absURL }}"></script>

with this

<script src="{{ $js.Permalink }}"></script>

Try it:

git clone --single-branch -b hugo-forum-topic-34963 hugo-forum-topic-34963
cd hugo-forum-topic-34963
hugo server

Tested with current versions of Chrome and Firefox.

Thank you! I knew that there is something missing. Before the last refactoring, my code was like the solution, but for some reason, I have replaced this.

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