import.meta.env does not work for dependencies when running in development
See original GitHub issueDescribe the bug
I’m trying to pass some environment variables to parts of my modular application.
For that I have some import.meta.env
statements in my dependencies.
When I build it in production, all seems to work fine; the import.meta.env
statements
are statically replaced with their actual values and all is well.
This is not consistent with the behaviour I’m seeing when running this in development.
Here I am getting an undefined error. It looks like, contrary to production mode, that in development
only the actual project gets its import.meta.env
filled in.
Is there any way to circumvent this? Is this intended behaviour?
Reproduction
- Make a project
- Add a dependency using
import.meta.env
- Build for production -> no problem
- Run development server -> 💥
System Info
System:
OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster)
CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
Memory: 1.54 GB / 15.33 GB
Container: Yes
Shell: 5.7.1 - /usr/bin/zsh
Binaries:
Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
Yarn: 1.22.0 - ~/.yarn/bin/yarn
npm: 8.1.4 - ~/.nvm/versions/node/v14.15.4/bin/npm
Browsers:
Chrome: 94.0.4606.81
Firefox: 78.15.0esr
npmPackages:
vite: ^2.6.14 => 2.7.1
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Typescript Types for import.meta.env - vue.js - Stack Overflow
I've tried the following but does not work. declare global { namespace NodeJS { interface ImportMeta { GITHUB_AUTH_TOKEN: string; NODE_ENV: ' ...
Read more >import-meta-env-webpack-plugin - npm package - Snyk
We found a way for you to contribute to the project! Looks like import-meta-env-webpack-plugin is missing a security policy.
Read more >Server-Side Rendering - Vite
if (import.meta.env.SSR) { // ... server only logic }. This is statically replaced during build so it will allow tree-shaking of unused branches....
Read more >rollup.js
Importing CommonJS · Publishing ES Modules ... This is not supported for native ES modules. ... npm run build -- --environment BUILD:development.
Read more >snowpack.config.js
Snowpack won't install them and will ignore them when resolving imports. ... you can run Snowpack with --polyfill-node (or installOptions.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pielambr I was thinking to use
define
too, but rejected this idea for some reasonable argument I successfully forgot 😄 I finished my setup, works fine, hit me in case you need help. Few more insights: Useresolve.dedupe
for same dependencies with parts. I use npm link for local development, it “steals” dependencies of linked package (well known not resolved bug https://github.com/npm/npm/issues/10343). Was trying to resolve it different ways, ended up with installation of “disappearing” dependencies locally, even though they’re not imported anywhere directly.I had not solved it, was thinking of switching over to https://vitejs.dev/config/#define to solve it, but will try the approach in your last comment, thanks!