js.BUILD

I am attempting to use js.build in my code, but I’m encountering a “Parsing error: ‘import’ and ‘export’ may appear only with ‘sourceType: module’” in my JavaScript file.

My code:

.HTML

  {{ $js := resources.Get "/js/index.js" | js.Build (dict "params" (dict "list" .Site.Params.variable)) }}

.js

import * as params from ‘@params’;

console.log(params.list);

.config.yaml

params:
  variable: ["x,y"]

Hugo Vertion: 0.121.1

Have any idea where I made a mistake?

Rename your Javascript to .mjs instead of .js, or change the type in your package.json to module. The issue is not with Hugo but how node/npm (and thus esbuild) handle files. If you want to force a module javascript file rename it to .mjs, if you want to force a commonJS file use .cjs. If you use .js as file extension then esbuild is checking the package.json for the type attribute, if it is not set it assumes commonjs.