Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to import netlify environment variable in assets/js/custom.js file #12586

Closed
MittalShruti opened this issue Jun 9, 2024 · 2 comments
Closed

Comments

@MittalShruti
Copy link

I am using this hugo theme flowbite-admin-dashboard. It uses webpack as well. This is how it builds the website:

"build": "NODE_ENV=production && run-s build:webpack build:hugo && rm /opt/build/repo/**/*.map",
"build:hugo": "hugo --destination=./public",
"build:webpack": "webpack --mode=production",
"build:styles": "npx tailwindcss -i ./src/style.css -o ./dist/css/flowbite.css",

My site is hosted on netlify. There I use npm run build in production.

I want to access environment variables (stored in netlify) in asset/js/custom.js file.

I tried adding this to layouts/_default/baseof.html file

  {{ $env := dict "encrypt" (getenv "HUGO_PARAMS_ENCRYPTION_KEY_HEX") }}
  {{ $js := resources.GetMatch "js/custom.js" | js.Build (dict "params" $env) }}
  <script async src="{{ $js.RelPermalink }}"></script>

in asset/js/custom.js file when i do
import params from "@params"
I get this error

custom.js:2 Uncaught Error: Cannot find module '@params'
    at webpackMissingModule (custom.js:2:50)
    at eval (custom.js:2:132)
    at ./assets/js/custom.js (app.bundle.js:19:1)
    at __webpack_require__ (bootstrap:19:1)
    at eval (index.js:11:78)
    at ./src/index.js (app.bundle.js:169:1)
    at __webpack_require__ (bootstrap:19:1)

I am new to hugo, how should i go about using env variables in .js file.

@jmooring
Copy link
Member

jmooring commented Jun 9, 2024

Please use the forum (https://discourse.gohugo.io/) for questions and troubleshooting. We prefer to use GitHub for verified bugs and vetted enhancements. Thanks.

@jmooring jmooring closed this as completed Jun 9, 2024
Copy link

github-actions bot commented Jul 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants