Skip to content

feat: move defineEnvVars to @sveltejs/kit/env - #16378

Merged
Rich-Harris merged 3 commits into
mainfrom
move-defineenvvars
Jul 17, 2026
Merged

feat: move defineEnvVars to @sveltejs/kit/env#16378
Rich-Harris merged 3 commits into
mainfrom
move-defineenvvars

Conversation

@Rich-Harris

@Rich-Harris Rich-Harris commented Jul 17, 2026

Copy link
Copy Markdown
Member

Realised that it'll be easier to do this in stages, deprecating it first rather than erroring immediately (#16375). This way we can keep the docs intact, and allow svelte.dev to continue building.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 17, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from d90d2b8:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/d90d2b8d095106dad63b574659d107862efa7008

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16378

@changeset-bot

changeset-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d90d2b8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional Suggestion:

The new @sveltejs/kit/env entry point (exporting defineEnvVars) is referenced by docs and JSDoc but is never wired into the package exports map or the type generation script, making import { defineEnvVars } from '@sveltejs/kit/env' throw ERR_PACKAGE_PATH_NOT_EXPORTED.

Fix on Vercel

vercel Bot and others added 2 commits July 17, 2026 02:51
…s`) is referenced by docs and JSDoc but is never wired into the package `exports` map or the type generation script, making `import { defineEnvVars } from '@sveltejs/kit/env'` throw `ERR_PACKAGE_PATH_NOT_EXPORTED`.

This commit fixes the issue reported at packages/kit/package.json:106

## Bug

The PR adds a new module `packages/kit/src/exports/env/index.js` exporting `defineEnvVars`, and updates docs (`documentation/docs/20-core-concepts/70-environment-variables.md`, 8 usages) and JSDoc/type links (`public.d.ts`, `types/index.d.ts`) to instruct users to `import { defineEnvVars } from '@sveltejs/kit/env'`.

However, the subpath was never registered:

1. **`packages/kit/package.json` `exports` map** had no `./env` entry. Node resolves subpath imports strictly against the `exports` field, so `import ... from '@sveltejs/kit/env'` throws `ERR_PACKAGE_PATH_NOT_EXPORTED` at runtime/build time. Concrete trigger: any consumer following the new docs example would fail immediately on import.

2. **`packages/kit/scripts/generate-dts.js` `modules` map** had no `'@sveltejs/kit/env'` entry, so `pnpm generate:types` did not emit a `declare module '@sveltejs/kit/env'` block. The docs link `@sveltejs-kit-env#defineEnvVars` and TS resolution both depend on that generated declaration.

## Fix

- Added a `./env` entry to the `exports` map mirroring the `./hooks` convention:
  ```json
  "./env": {
      "types": "./types/index.d.ts",
      "import": "./src/exports/env/index.js"
  }
  ```
- Added `'@sveltejs/kit/env': 'src/exports/env/index.js'` to the `modules` map in `generate-dts.js`.
- Regenerated `types/index.d.ts` (`node scripts/generate-dts.js`), which now contains the `declare module '@sveltejs/kit/env'` block exporting `defineEnvVars`.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good but not merging because I'm not 100% sure if you meant this to go into main (because of the "deprecate first, remove later" bit)

@Rich-Harris
Rich-Harris changed the base branch from version-3 to main July 17, 2026 11:58
@Rich-Harris

Copy link
Copy Markdown
Member Author

Whoops, yes I did - was branched off main locally but forgot to replace the default branch

@Rich-Harris
Rich-Harris merged commit 78f28f1 into main Jul 17, 2026
20 of 21 checks passed
@Rich-Harris
Rich-Harris deleted the move-defineenvvars branch July 17, 2026 12:54
@github-actions github-actions Bot mentioned this pull request Jul 17, 2026
Rich-Harris pushed a commit that referenced this pull request Jul 17, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @sveltejs/kit@2.70.0

### Minor Changes

- feat: move `defineEnvVars` to `@sveltejs/kit/env`
([#16378](#16378))

### Patch Changes

- fix: enable CSRF protection in builds with a non-production `NODE_ENV`
value ([#16313](#16313))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Rich-Harris pushed a commit that referenced this pull request Jul 18, 2026
As invited in
[#16195](#16195 (comment)),
`defineEnvVars` now accepts a function in place of a Standard Schema.
The contract mirrors `defineParams`: the function returns the (possibly
transformed) value, or `undefined` if the value is invalid. It can also
throw, in which case the error's message appears in the startup report,
since a bare `undefined` can't carry one.

Throwing is the only rejection path, returning any value including
`undefined` accepts it, so a function validator can describe an optional
variable without a fallback.

`defineEnvVars` normalizes functions to standard schemas, so `validate`
and the generated `env.d.ts` types are unchanged (the return type is
mapped like `DefinedParams`). Also adds the `@sveltejs/kit/env` tsconfig
paths entry that #16378 didn't need yet.

Built on top of #16401.

---

### Please don't delete this checklist! Before submitting the PR, please
make sure you do the following:
- [x] It's really useful if your PR references an issue where it is
discussed ahead of time. In many cases, features are absent for a
reason. For large changes, please create an RFC:
https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it
solves.
- [x] Ideally, include a test that fails without this PR but passes with
it.

### Tests
- [x] Run the tests with `pnpm test` and lint the project with `pnpm
lint` and `pnpm check`

### Changesets
- [x] If your PR makes a change that should be noted in one or more
packages' changelogs, generate a changeset by running `pnpm changeset`
and following the prompts. Changesets that add features should be
`minor` and those that fix bugs should be `patch`. Please prefix
changeset messages with `feat:`, `fix:`, or `chore:`.

### Edits

- [x] Please ensure that 'Allow edits from maintainers' is checked. PRs
without this option may be closed.
@Nic-Polumeyv Nic-Polumeyv mentioned this pull request Jul 20, 2026
6 tasks
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Jul 27, 2026
##### [v2.70.1](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2701)

##### Patch Changes

- docs: update links to hooks documentation ([#16417](sveltejs/kit#16417))
##### [v2.70.0](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2700)

##### Minor Changes

- feat: move `defineEnvVars` to `@sveltejs/kit/env` ([#16378](sveltejs/kit#16378))

##### Patch Changes

- fix: enable CSRF protection in builds with a non-production `NODE_ENV` value ([#16313](sveltejs/kit#16313))
##### [v2.69.3](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2693)

##### Patch Changes

- fix: detect destructured `load` and `actions` exports during type generation ([#16329](sveltejs/kit#16329))

- fix: ensure CSS URL references are absolute when `paths.relative` is `false` ([#16315](sveltejs/kit#16315))

- fix: align MAX\_COOKIE\_SIZE with RFC 6265bis ([#16322](sveltejs/kit#16322))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Jul 27, 2026
##### [v2.70.1](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2701)

##### Patch Changes

- docs: update links to hooks documentation ([#16417](sveltejs/kit#16417))
##### [v2.70.0](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2700)

##### Minor Changes

- feat: move `defineEnvVars` to `@sveltejs/kit/env` ([#16378](sveltejs/kit#16378))

##### Patch Changes

- fix: enable CSRF protection in builds with a non-production `NODE_ENV` value ([#16313](sveltejs/kit#16313))
##### [v2.69.3](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2693)

##### Patch Changes

- fix: detect destructured `load` and `actions` exports during type generation ([#16329](sveltejs/kit#16329))

- fix: ensure CSS URL references are absolute when `paths.relative` is `false` ([#16315](sveltejs/kit#16315))

- fix: align MAX\_COOKIE\_SIZE with RFC 6265bis ([#16322](sveltejs/kit#16322))
gwennlbh pushed a commit to cigaleapp/cigale that referenced this pull request Jul 27, 2026
##### [v2.70.1](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2701)

##### Patch Changes

- docs: update links to hooks documentation ([#16417](sveltejs/kit#16417))
##### [v2.70.0](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2700)

##### Minor Changes

- feat: move `defineEnvVars` to `@sveltejs/kit/env` ([#16378](sveltejs/kit#16378))

##### Patch Changes

- fix: enable CSRF protection in builds with a non-production `NODE_ENV` value ([#16313](sveltejs/kit#16313))
##### [v2.69.3](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2693)

##### Patch Changes

- fix: detect destructured `load` and `actions` exports during type generation ([#16329](sveltejs/kit#16329))

- fix: ensure CSS URL references are absolute when `paths.relative` is `false` ([#16315](sveltejs/kit#16315))

- fix: align MAX\_COOKIE\_SIZE with RFC 6265bis ([#16322](sveltejs/kit#16322))

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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