Skip to content

vd-aloker/telefunc-issue-environment

Repository files navigation

Issue with $env/dynamic/private

The env object in $env/dynamic/private is empty when accessed from a telefunc function.

Here's an example telefunc function:

import { env } from '$env/dynamic/private';
import { dumpEnvironmentVariables } from '$lib/server/utils';

export async function onGetRemoteSecret() {
	dumpEnvironmentVariables(`Telefunc function: onGetRemoteSecret`);
	return {
		// read secret in idiomatic SvelteKit way
		secretFromTelefunc: env.MY_SECRET,
		// read secret in Node.js way
		secretFromTelefuncProcessEnv: process.env.MY_SECRET
	};
}

Here's an example load function that works similarly:

import { env } from '$env/dynamic/private';
import { dumpEnvironmentVariables } from '$lib/server/utils';

export const load = () => {
	dumpEnvironmentVariables(`Page server load function`);
	return {
		secretFromLoadFunction: env.MY_SECRET
	};
};

When running in the dev server this works as expected: alt text

However, using @sveltejs/adapter-node and running the compiled app using node build this is the result:

alt text

Things to note:

  • Accessing the environment variable in a load function works
  • Accessing the environment variable in a telefunc function only works in dev, not in the built application.
  • However, the variable is accessible from process.env in both cases (though that isn't the idiomatic way)

Expected behavior

Accessing env from $env/dynamic/private should work identicially in dev and prod environments and especially identically when used in a load function and when accessed from a telefunc function. On the server, all environment variables in env should be dumped to the console for both the telefunc function and the load function.

Actual behavior

Only for the telefunc function:

  • env in $env/dynamic/private is empty when accessed from a telefunc function
  • No value is returned from env.MY_SECRET
  • No environt variable is dumped to the console

Steps to reproduce

  • Clone repo
  • npm install
  • npm run build
  • Set environment variables using SET, $env["..."], export ... or whatever your environment needs:
    • MY_SECRET="My little secret"
    • ORIGIN=http://localhost:3000
    • NODE_ENV=production (apparently telefunc needs this)
  • node build to start the application
  • Open browser at http://localhost:3000
  • Have a look at the rendered data and the server console

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published