Env vars from dashboard not loading in Cloudflare Pages site with wrangler.toml

For Workes & Pages, what is the name of the domain?

N/A

What is the error number?

N/A

What is the error message?

Build environment variables: (none found)

What is the issue or error you’re encountering

(Non-encrypted) environment variables are not loading in Pages site when wrangler.toml (for pages) is defined.

What steps have you taken to resolve the issue?

I have tried doing a workaround by adding [keys] or [secrets] to wrangler.toml, but it is very bad practice to add secrets to files in a Git repository.

I tried converting the env vars to secrets work. Normal environment variables are not being loaded.

It is still annoying that non-encrypted environment variables defined in the dashboard are not loaded.

I believe that non-encrypted environment variables still work if you do not define a wrangler.toml. Cannot test right now because the app I am building breaks if it does not have compatibility_flags = ["nodejs_compat"] flag enabled.

What are the steps to reproduce the issue?

Create SvelteKit app.

Create wrangler.toml

name = "my_app"
pages_build_output_dir = ".svelte-kit/cloudflare"
compatibility_flags = ["nodejs_compat"]

[[kv_namespaces]]
binding = "KV"
id = "<UUID>"

[[d1_databases]]
binding = "DB"
database_name = "DB"
database_id = "<UUID>"

Connect Cloudflare Pages to Github.

Update: Changed all variables to encrypted secrets.

Now getting a build error with no indication of the error:

...
|23:48:34.937|Run npm run preview to preview your production build locally.|
|---|---|
|23:48:34.939||
|23:48:34.939|> Using @sveltejs/adapter-cloudflare|
|23:48:35.459|  ✔ done|
|23:48:35.607|Finished|
|23:48:35.607|Found _worker.js in output directory. Uploading.|
|23:48:36.774|✨ Compiled Worker successfully|
|23:48:36.827|Found _routes.json in output directory. Uploading.|
|23:48:36.841|Validating asset output directory|
|23:48:38.842|Deploying your site to Cloudflare's global network...|
|23:48:41.386|Parsed 2 valid header rules.|
|23:48:42.893|Uploading... (36/47)|
|23:48:44.620|Uploading... (40/47)|
|23:48:44.698|Uploading... (43/47)|
|23:48:46.284|Uploading... (47/47)|
|23:48:46.285|✨ Success! Uploaded 11 files (36 already uploaded) (3.89 sec)|
|23:48:46.285||
|23:48:46.515|✨ Upload complete!|
|23:48:48.472|Success: Assets published!|
|23:48:49.701|Error: Failed to publish your Function. Got error: Unknown internal error occurred.|

May need to figure out a workaround without using wrangler.toml

(After deleting wrangler.toml) Adding nodejs_compat configured via dashboard (Settings > Functions > Compatibility flags) does not appear to do anything, so I am not sure if there is much I can do.

Note: keep_vars gives error: Configuration file for Pages projects does not support "keep_vars"

The environment variables issue has an easily work-around by changing to encrypted secrets.

However, I still have a problem in that I need to use wrangler.toml (for nodejs_compat flag), but I get a completely unknown and undescribed build error: |23:48:49.701|Error: Failed to publish your Function. Got error: Unknown internal error occurred.| when I have my wrangler.toml defined.

So I am blocked for now.

Blocking problem for me right now is:

When you create a SvelteKit app, and then create a wrangler.toml, it gives an unknown internal build error:

2024-08-24T15:28:22.064528Z	Cloning repository...
2024-08-24T15:28:22.639903Z	From https://github.com/plasmatech8/test-sveltekit-app
2024-08-24T15:28:22.64039Z	 * branch            4b756be076c36992fd9faf7c3b298744b772a16a -> FETCH_HEAD
2024-08-24T15:28:22.640496Z	
2024-08-24T15:28:22.670427Z	HEAD is now at 4b756be Add wrangler.toml
2024-08-24T15:28:22.670914Z	
2024-08-24T15:28:22.756292Z	
2024-08-24T15:28:22.757107Z	Using v2 root directory strategy
2024-08-24T15:28:22.781713Z	Success: Finished cloning repository files
2024-08-24T15:28:24.40018Z	Checking for configuration in a wrangler.toml configuration file (BETA)
2024-08-24T15:28:24.401098Z	
2024-08-24T15:28:24.401217Z	Found wrangler.toml file. Reading build configuration...
2024-08-24T15:28:24.406454Z	pages_build_output_dir: .svelte-kit/cloudflare
2024-08-24T15:28:24.406643Z	Build environment variables: (none found)
2024-08-24T15:28:24.497058Z	Successfully read wrangler.toml file.
2024-08-24T15:28:25.844839Z	Detected the following tools from environment: [email protected], [email protected]
2024-08-24T15:28:25.845485Z	Installing nodejs 21.7.3
2024-08-24T15:28:26.36871Z	Trying to update node-build... ok
2024-08-24T15:28:26.470537Z	To follow progress, use 'tail -f /tmp/node-build.20240824152826.556.log' or pass --verbose
2024-08-24T15:28:26.574816Z	Downloading node-v21.7.3-linux-x64.tar.gz...
2024-08-24T15:28:26.697909Z	-> https://nodejs.org/dist/v21.7.3/node-v21.7.3-linux-x64.tar.gz
2024-08-24T15:28:28.459058Z	
2024-08-24T15:28:28.459421Z	WARNING: node-v21.7.3-linux-x64 is past its end of life and is now unsupported.
2024-08-24T15:28:28.459582Z	It no longer receives bug fixes or security updates.
2024-08-24T15:28:28.459787Z	
2024-08-24T15:28:28.459921Z	Installing node-v21.7.3-linux-x64...
2024-08-24T15:28:28.853541Z	Installed node-v21.7.3-linux-x64 to /opt/buildhome/.asdf/installs/nodejs/21.7.3
2024-08-24T15:28:28.853805Z	
2024-08-24T15:28:29.978344Z	Installing project dependencies: npm clean-install --progress=false
2024-08-24T15:28:32.869008Z	npm WARN deprecated [email protected]: Rimraf versions prior to v4 are no longer supported
2024-08-24T15:28:32.912904Z	npm WARN deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
2024-08-24T15:28:32.924181Z	npm WARN deprecated [email protected]: Glob versions prior to v9 are no longer supported
2024-08-24T15:28:34.977875Z	
2024-08-24T15:28:34.978188Z	added 220 packages, and audited 221 packages in 4s
2024-08-24T15:28:34.978346Z	
2024-08-24T15:28:34.978431Z	51 packages are looking for funding
2024-08-24T15:28:34.978501Z	  run `npm fund` for details
2024-08-24T15:28:34.979444Z	
2024-08-24T15:28:34.979671Z	found 0 vulnerabilities
2024-08-24T15:28:34.993559Z	Executing user command: npm run build
2024-08-24T15:28:35.569982Z	
2024-08-24T15:28:35.570235Z	> [email protected] build
2024-08-24T15:28:35.57041Z	> vite build
2024-08-24T15:28:35.570534Z	
2024-08-24T15:28:36.458454Z	e[36mvite v5.4.2 e[32mbuilding SSR bundle for production...e[36me[39m
2024-08-24T15:28:36.498281Z	transforming...
2024-08-24T15:28:36.888213Z	e[32m✓e[39m 80 modules transformed.
2024-08-24T15:28:36.936017Z	rendering chunks...
2024-08-24T15:28:37.494401Z	e[36mvite v5.4.2 e[32mbuilding for production...e[36me[39m
2024-08-24T15:28:37.496805Z	transforming...
2024-08-24T15:28:37.825251Z	e[32m✓e[39m 62 modules transformed.
2024-08-24T15:28:37.855309Z	rendering chunks...
2024-08-24T15:28:37.870452Z	computing gzip size...
2024-08-24T15:28:37.875903Z	e[2m.svelte-kit/output/client/e[22me[32m_app/version.json                            e[39me[1me[2m 0.03 kBe[22me[1me[22me[2m │ gzip:  0.05 kBe[22m
2024-08-24T15:28:37.876227Z	e[2m.svelte-kit/output/client/e[22me[32m.vite/manifest.json                          e[39me[1me[2m 2.25 kBe[22me[1me[22me[2m │ gzip:  0.44 kBe[22m
2024-08-24T15:28:37.876345Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/entry/start.D4Vs9eo8.js       e[39me[1me[2m 0.07 kBe[22me[1me[22me[2m │ gzip:  0.08 kBe[22m
2024-08-24T15:28:37.876414Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/nodes/0.V07vbrzA.js           e[39me[1me[2m 0.60 kBe[22me[1me[22me[2m │ gzip:  0.39 kBe[22m
2024-08-24T15:28:37.876971Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/nodes/2.DYBzjjm4.js           e[39me[1me[2m 0.69 kBe[22me[1me[22me[2m │ gzip:  0.45 kBe[22m
2024-08-24T15:28:37.877199Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/nodes/1.D3qCuM8c.js           e[39me[1me[2m 1.02 kBe[22me[1me[22me[2m │ gzip:  0.60 kBe[22m
2024-08-24T15:28:37.878613Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/chunks/scheduler.BvLojk_z.js  e[39me[1me[2m 2.16 kBe[22me[1me[22me[2m │ gzip:  1.02 kBe[22m
2024-08-24T15:28:37.878825Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/chunks/index.DFTQtrJW.js      e[39me[1me[2m 5.43 kBe[22me[1me[22me[2m │ gzip:  2.30 kBe[22m
2024-08-24T15:28:37.87898Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/entry/app.Bhai11aE.js         e[39me[1me[2m 6.02 kBe[22me[1me[22me[2m │ gzip:  2.44 kBe[22m
2024-08-24T15:28:37.880138Z	e[2m.svelte-kit/output/client/e[22me[36m_app/immutable/chunks/entry.Gr7MhXcC.js      e[39me[1me[2m27.41 kBe[22me[1me[22me[2m │ gzip: 10.83 kBe[22m
2024-08-24T15:28:37.880282Z	e[32m✓ built in 382mse[39m
2024-08-24T15:28:38.368007Z	e[2m.svelte-kit/output/server/e[22me[32m.vite/manifest.json                 e[39me[1me[2m 1.64 kBe[22me[1me[22m
2024-08-24T15:28:38.368529Z	e[2m.svelte-kit/output/server/e[22me[36mentries/fallbacks/layout.svelte.js  e[39me[1me[2m 0.24 kBe[22me[1me[22m
2024-08-24T15:28:38.368697Z	e[2m.svelte-kit/output/server/e[22me[36minternal.js                         e[39me[1me[2m 0.31 kBe[22me[1me[22m
2024-08-24T15:28:38.368822Z	e[2m.svelte-kit/output/server/e[22me[36mentries/pages/_page.svelte.js       e[39me[1me[2m 0.37 kBe[22me[1me[22m
2024-08-24T15:28:38.369127Z	e[2m.svelte-kit/output/server/e[22me[36mentries/fallbacks/error.svelte.js   e[39me[1me[2m 1.66 kBe[22me[1me[22m
2024-08-24T15:28:38.369341Z	e[2m.svelte-kit/output/server/e[22me[36mchunks/ssr.js                       e[39me[1me[2m 2.82 kBe[22me[1me[22m
2024-08-24T15:28:38.369437Z	e[2m.svelte-kit/output/server/e[22me[36mchunks/exports.js                   e[39me[1me[2m 5.94 kBe[22me[1me[22m
2024-08-24T15:28:38.369537Z	e[2m.svelte-kit/output/server/e[22me[36mchunks/internal.js                  e[39me[1me[2m 6.00 kBe[22me[1me[22m
2024-08-24T15:28:38.369662Z	e[2m.svelte-kit/output/server/e[22me[36mindex.js                            e[39me[1me[2m93.25 kBe[22me[1me[22m
2024-08-24T15:28:38.369733Z	e[32m✓ built in 1.89se[39m
2024-08-24T15:28:38.36982Z	
2024-08-24T15:28:38.369947Z	Run npm run preview to preview your production build locally.
2024-08-24T15:28:38.37365Z	
2024-08-24T15:28:38.373852Z	> Using @sveltejs/adapter-auto
2024-08-24T15:28:38.697458Z	Installing @sveltejs/adapter-cloudflare...
2024-08-24T15:28:42.736211Z	
2024-08-24T15:28:42.736505Z	added 6 packages, and audited 227 packages in 4s
2024-08-24T15:28:42.736689Z	
2024-08-24T15:28:42.736808Z	51 packages are looking for funding
2024-08-24T15:28:42.736928Z	  run `npm fund` for details
2024-08-24T15:28:42.737776Z	
2024-08-24T15:28:42.737948Z	found 0 vulnerabilities
2024-08-24T15:28:42.760797Z	Successfully installed @sveltejs/adapter-cloudflare.
2024-08-24T15:28:42.762116Z	
2024-08-24T15:28:42.762304Z	If you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with @sveltejs/adapter-cloudflare. This will give you faster and more robust installs, and more control over deployment configuration.
2024-08-24T15:28:42.762443Z	
2024-08-24T15:28:43.309685Z	  ✔ done
2024-08-24T15:28:43.378377Z	Finished
2024-08-24T15:28:43.379045Z	Found _worker.js in output directory. Uploading.
2024-08-24T15:28:44.43315Z	✨ Compiled Worker successfully
2024-08-24T15:28:44.534232Z	Found _routes.json in output directory. Uploading.
2024-08-24T15:28:44.570864Z	Validating asset output directory
2024-08-24T15:28:46.237508Z	Deploying your site to Cloudflare's global network...
2024-08-24T15:28:47.872691Z	Parsed 2 valid header rules.
2024-08-24T15:28:49.273037Z	Uploading... (5/12)
2024-08-24T15:28:49.832913Z	Uploading... (7/12)
2024-08-24T15:28:49.985022Z	Uploading... (9/12)
2024-08-24T15:28:50.164278Z	Uploading... (12/12)
2024-08-24T15:28:50.164762Z	✨ Success! Uploaded 7 files (5 already uploaded) (1.37 sec)
2024-08-24T15:28:50.164924Z	
2024-08-24T15:28:50.48329Z	✨ Upload complete!
2024-08-24T15:28:52.468593Z	Success: Assets published!
2024-08-24T15:28:53.286647Z	Error: Failed to publish your Function. Got error: Unknown internal error occurred.

No idea how to resolve.

Changed adapter-cloudflare to 3.0.2 and fixed the Failed to publish your Function problem as far as I am aware.

Super confused because it stopped working again and getting Error: Failed to publish your Function. Got error: Unknown internal error occurred. for no seemingly reason. Will have to do further testing later.

After some tinkering around, I managed to get it working with compatibility_date = "2024-08-20", with SvelteKit cloudflare adapter 3.0.2, and fixed the d1 and r2 bindings. Not sure if other changes helped (e.g. npm audit fix, using different axios version, remove KV).

Comments above are not mostly not relevant to the topic of the discussion.

Topic is about non-encrypted env vars not being loaded.

And I can now see the banner: " Environment variables for this project’s production environment are being managed through wrangler.toml. Only Secrets (encrypted variables) can be added or removed through the Dashboard. Learn more" which communicates that we cannot use normal non-encrypted variables anymore.

1 Like

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