question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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_

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rgossiauxcommented, Oct 9, 2022

Sure, I’m working around this by just manually replacing process.env.NODE_ENV in the package script in package.json:

https://github.com/rgossiaux/svelte-headlessui/blob/f431f891271291c8db7c75fe00c7c19b2542a21d/package.json#L22

Pretty janky but it gets the job done for my use case.

1reaction
dodiameercommented, Jan 21, 2022

Possibly related to #2572 - It would help fix this issue because (assuming bundling would use vite or rollup) you could use Vite’s define option or @rollup/plugin-replace to do the replacement, but as of right now using either of them won’t work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is process.env.NODE_ENV undefined? - Stack Overflow
process.env is a reference to your environment, so you have to set the variable there. To set an environment variable in Windows: SET...
Read more >
Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
Build environment variables | Netlify Docs
Netlify environment variables are accessible during your build. This allows you to change behaviors based on deploy parameters or to include information you ......
Read more >
Environment variables in Compose | Docker Documentation
The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy . Both...
Read more >
Using environment variables in React | by Sam Pakvis - Medium
When you don't have a server-side programming background, environment ... run your npm run dev command, go to your React component and use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found