Skip to content

fix(dev): avoid dynamic import.meta.env access in dev notice helper#403

Open
stickerdaniel wants to merge 1 commit into
mainfrom
fix/dev-notice-import-meta-env
Open

fix(dev): avoid dynamic import.meta.env access in dev notice helper#403
stickerdaniel wants to merge 1 commit into
mainfrom
fix/dev-notice-import-meta-env

Conversation

@stickerdaniel
Copy link
Copy Markdown
Owner

@stickerdaniel stickerdaniel commented May 23, 2026

SvelteKit dev was crashing every request with HTTP 500 because Vite's module-runner now rejects dynamic property access on import.meta.env ("Dynamic access of import.meta.env is not supported"). The crash happened in src/lib/dev/notice.ts via hooks.server.ts:21, before any +error.svelte could render, so every page on bun run dev looked broken.

The notice helper had to dodge import.meta.env.DEV originally because the module is imported from both the Vite tsconfig (SvelteKit, browser) and the Convex tsconfig (Convex isolates), and the Convex tsconfig has no Vite types. The previous workaround was a cast to a partial type, but the runtime access was still dynamic, which is what Vite's module-runner objects to.

Switched the SvelteKit/browser dev check from import.meta.env.DEV to process.env.NODE_ENV === 'development'. SvelteKit SSR gets NODE_ENV from Node at runtime, and Vite statically replaces process.env.NODE_ENV in the browser bundle, so the check works in both environments. @types/node covers it under both tsconfigs, no @ts-expect-error needed. The Convex-scope branch is unchanged.

The Vitest case now sets NODE_ENV='development' in beforeEach and restores it in afterEach so the live "logs a vite-public feature" assertion still exercises the dev path.

bun scripts/static-checks.ts and bun run test:unit src/lib/dev/notice.test.ts pass. Verified live: curl http://localhost:5175/en returned HTTP 500 before the fix and HTTP 200 after, on a fresh dev server in this worktree.

Vite's module-runner rejects dynamic property access on import.meta.env
("Dynamic access of import.meta.env is not supported"), so every
SvelteKit dev request was crashing with a 500 in hooks.server.ts before
any +error.svelte could render.

Switch the SvelteKit/browser dev detection from
  `(import.meta as { env?: { DEV?: boolean } }).env?.DEV`
to
  `process.env.NODE_ENV === 'development'`.

SvelteKit SSR reads NODE_ENV from Node at runtime; the browser bundle
gets it via Vite's static define replacement. No Vite-specific
import.meta access, so no dynamic-access rejection, and the module
type-checks cleanly under both the Vite tsconfig and the Convex
tsconfig without @ts-expect-error escape hatches.

The Vitest case sets NODE_ENV='development' in beforeEach and restores
it in afterEach so the existing "logs a vite-public feature" test still
exercises the live dev path.
@stickerdaniel stickerdaniel marked this pull request as ready for review May 23, 2026 23:10
Copilot AI review requested due to automatic review settings May 23, 2026 23:10
@qodo-code-review
Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

Copy link
Copy Markdown
Owner Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
saas-starter f8c1ddd Commit Preview URL

Branch Preview URL
May 23 2026, 11:12 PM

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a SvelteKit dev-server crash caused by Vite’s module-runner rejecting dynamic import.meta.env access in the shared devNotice helper (which runs early via hooks.server.ts), and updates the unit test to keep exercising the dev-only path.

Changes:

  • Replace the non-Convex dev check in isDev() from a dynamically-accessed import.meta.env value to process.env.NODE_ENV === 'development'.
  • Update the devNotice Vitest to set/restore NODE_ENV so the dev path is covered.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/lib/dev/notice.ts Updates dev-detection logic and documentation to avoid dynamic import.meta.env access.
src/lib/dev/notice.test.ts Adjusts unit tests to force the dev path by setting NODE_ENV during tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/dev/notice.ts
Comment on lines +28 to +33
// SvelteKit SSR reads NODE_ENV from Node; the browser gets it via Vite's
// static define replacement at build time. Avoids the dynamic
// `import.meta.env` access that Vite's module-runner rejects, and
// type-checks under both the Vite tsconfig and the Convex tsconfig
// (which has @types/node but not Vite's import.meta augmentations).
return typeof process !== 'undefined' && process.env?.NODE_ENV === 'development';
Comment thread src/lib/dev/notice.ts
Comment on lines +15 to +17
* **Active only in dev** (`NODE_ENV === 'development'` under SvelteKit and
* in the browser, `LOCAL_CONVEX_DEV` on the Convex backend). The helper is
* a no-op in production builds and on cloud deployments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants