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.

import.meta.env does not work for dependencies when running in development

See original GitHub issue

Describe 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
souljorjecommented, Dec 21, 2021

@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: Use resolve.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.

1reaction
pielambrcommented, Dec 21, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

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