Access value from Config.yaml to .js

I have a small requirement in my code.

Say, for example, I have a variable x = [a, b, c, ...] in the config.yaml file. Now, I’m trying to access this variable in a common .js file.

How can I do this? Is this possible in Hugo?

You can inline your JS.

Or build your JS: Access template variables in JS - #4 by regis

Have a look here: js.Build | Hugo

Thanks @nfriedli ,I tried this, but I’m getting an error. I have attached my code and error screenshot below.

In .html file

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

in .js

` import * as params from '@params';

console.log(params.myparams)`

Error Screenshot

![image|690x56](upload://csCYxRvb7MoPxEeUohy9rRT8DlO.png

and my hugo version is 0.82.0

That version does not support params in JS. You need to upgrade to the latest version of Hugo (0.123.8)

1 Like

Ok thanks :+1:

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