sveltejs / sveltejs/kit

The `package` command does not replace `process.env.*`

Open
#3,480 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pkg:svelte-package
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

When you run npm run package in a SvelteKit project, process.env references (like process.env.NODE_ENV) are not statically replaced; the files that are output still contain the process.env references.

I don't know if there are cases when this is desirable, but in my case it is not. My library has a few checks for process.env.NODE_ENV === "test" which are only internal. I have a hard time imagining a case when a package user would want anything other than "production" from a third-party library.

In particular, this seems like it causes an issue with Svelte projects built with https://github.com/sveltejs/template where the packaging also does not handle process.env.

Reproduction

It's not a minimal example, but I ran into this with my component library: https://github.com/rgossiaux/svelte-headlessui

You can repro this by checking out & installing the repo, then running npm run package.

A user then reported that the resulting package is broken with https://github.com/sveltejs/template using this example repo: https://github.com/jerriclynsjohn/svelte-headlessui-test-repo

Logs

No response

System Info
System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 7.89 GB / 64.00 GB
    Shell: 3.3.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 16.8.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23841_1640400023703/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 7.21.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23841_1640400023703/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Safari: 15.2



### Severity

serious, but I can work around it

### Additional Information

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by checking out the reproduction repository and running npm run package to inspect the generated files for process.env references. Compare the output with the sveltejs/template consumer example and trace the package build configuration responsible. Done means the packaged library replaces the relevant process.env values and works in the consumer project.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.