Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vite-node should have different behavior with env vars than vite #5624

Open
4 tasks done
ethanjdiamond opened this issue Apr 26, 2024 · 1 comment
Open
4 tasks done

Comments

@ethanjdiamond
Copy link

Clear and concise description of the problem

In Vite's Env variables and modes documentation, there's a section on how Vite filters env variables that doesn't start with a specified prefix. This is what it says:

To prevent accidentally leaking env variables to the client, only variables prefixed with VITE_ are exposed to your Vite-processed code.

While that makes sense and is helpful when building a web bundle, it doesn't make as much sense for vite-node's use cases. In particular I have two use cases where it's highly inconvenient:

  • I'm running .ts scripts using vite-node via a hashbang in script mode (#!/usr/bin/env vite-node --script) that use child_process to run terminal commands. The PATH env var is getting filtered since it's not prefixed with VITE_, which makes running any terminal commands pretty much a no go.
  • I'm starting my express server using vite-node, and passing the port it should start on as an env var, and prefixing is inconvenient. Server-side is a safe environment for secrets.

Suggested solution

It'd be really helpful if vite-node followed how Next.js handles env var safety and didn't require a prefix in node environments. In Next, anything prefixed with NEXT_PUBLIC_ will be exposed to the browser, but there's no such restriction for the server.

Alternative

I considered potentially having a setting in vite.config.ts for vite-node that would allow you to set whether you needed a prefix for not, but in my project I use vite both to build my web app and as a script runner, so it'd be a little annoying to need two config files. I'd prefer vite-node to just allow through all env vars.

Additional context

No response

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Apr 26, 2024

I thought it should, but just verified that's not the case currently on vite-node.
https://stackblitz.com/edit/github-pjqhpg?file=repro.ts

On Vitest, there is a few tricks (such as #714) to make import.meta.env works mostly same as process.env, so this is probably working there, but something is missing for vite-node alone usage.

I thought you could at least use envPrefix: "", but Vite doesn't allow passing in such config as they assume its dangerous for web bundle use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: P2 - 3
Development

No branches or pull requests

2 participants