oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.19k stars 2.58k forks source link

import.meta.env support #4667

Open beepsoft opened 10 months ago

beepsoft commented 10 months ago

What is the problem this feature would solve?

Introducing access to .env variables via import.meta.env would make bun more compatible with existing tools (eg. Vite) and would help migrating to bun.

What is the feature you are proposing to solve the problem?

It would be great to have support for accessing .env variables via import.meta.env as in Vite: https://vitejs.dev/guide/env-and-mode.html

What alternatives have you considered?

No response

taranek commented 10 months ago

@Jarred-Sumner Any idea when this might happen? We (+ probably many other devs) can't migrate our tests to bun due to this issue

MarkLyck commented 9 months ago

@Jarred-Sumner Also having this issue, a lot of my components import something from my config file which includes:

const NODE_ENV = import.meta.env.NODE_ENV

resulting in an error when running tests:

TypeError: undefined is not an object (evaluating 'import.meta.env.NODE_ENV')
sciolist commented 9 months ago

As a workaround (worked for me at least) you should be able to create a file, say, fix-import-meta-env.js with the content import.meta.constructor.prototype.env = process.env then run your scripts with bun -r ./fix-import-meta-env.js my-script.ts.

Not something you'd want to keep in the long term. Would be nice to get it built in!

Jarred-Sumner commented 8 months ago

Runtime support for import.meta.env is merged, thanks to @otgerrogla. Not yet supported at build-time