Skip to content

imports from $env are undefined when imported in src/instrumentation.server.js #14286

Description

@jorisguffens

Describe the bug

With adapter-node and the experimental observability flag enabled:

Importing $env/dynamic/public in the src/instrumentation.server.ts file will work when running with vite dev but not when running the build in production with node ./build.

I was unable to verify it with vite preview as the instrumentation.server.ts is not used in this mode (#14275)

Reproduction

Additional information is presented in the README of this reproduction repo:
https://github.com/jorisguffens/sveltekit-otel-env-var-not-available

Short snippet of instrumentation.server.js

import { dev } from "$app/environment";
import { env } from "$env/dynamic/public";

console.log("dev", dev);
console.log("process.env", process.env.PUBLIC_APPINSIGHTS_CONNECTION_STRING);
console.log("$env/dynamic/public", env.PUBLIC_APPINSIGHTS_CONNECTION_STRING);

Console output of vite dev

dev true
process.env undefined
$env/dynamic/public InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://applicationinsights.azure.com/;LiveEndpoint=https://livediagnostics.monitor.azure.com/;ApplicationId= 00000000-0000-0000-0000-000000000000

Console output of node --env-file=.env ./build

dev false
process.env InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://applicationinsights.azure.com/;LiveEndpoint=https://livediagnostics.monitor.azure.com/;ApplicationId= 00000000-0000-0000-0000-000000000000
$env/dynamic/public undefined

Logs

System Info

@sveltejs/adapter-node: ^5.3.1 => 5.3.1
@sveltejs/kit: ^2.22.0 => 2.36.1
@sveltejs/vite-plugin-svelte: ^6.0.0 => 6.1.3
svelte: ^5.0.0 => 5.38.2
vite: ^7.0.4 => 7.1.3

Severity

annoyance

Additional Information

As a workaround I had to use process.env which was available in the production build but not when running with vite dev. This discrepancy can be confusing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    $envBugs related to the `$env` family of modulespkg:adapter-node

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions