🐛 BUG: __SNOWPACK_ENV__ is not defined when used in module
See original GitHub issueQuick checklist
- I am using the latest version of Snowpack and all plugins.
What package manager are you using?
yarn
What operating system are you using?
Linux
Describe the bug
Hi, I created a pretty straight forward project for an react app and I would like to use the @snowpack/plugin-dotenv plugin to load an enviroment file to configure my build for different deployments. I would like to have my separate constants.js
module which defines all kinds of required configuration parameters for the project and within this file I try to set some variables like
// derived information
const ENABLE_DEBUG = __SNOWPACK_ENV__.SNOWPACK_PUBLIC_ENVIRONMENT === 'XY'
// shortcut
const SOME = __SNOWPACK_ENV__.SNOWPACK_PUBLIC_SOME;
export { ENABLE_DEBUG, SOME };
As soon as I try to use the __SNOWPACK_ENV__
expression in i.e. the constants
module I will always get the error
Uncaught ReferenceError: __SNOWPACK_ENV__ is not defined
But when I try to access the variables i.e. in App.js
and Component.js
directly it will work fine. Unfortunately it is not really feasible to add expressions like the one above at every place in the code where I need them (thus the abbreviation/derived expression in my extra constants module).
It seems that snowpack does not substitute the expression in the file while building it. I don’t use any special configuration and no other packager or optimization plugin right now.
Steps to reproduce
- Use
npx create-snowpack-app
to create an app - Create a module ‘constants.js’ in the src directory
- Access the SNOWPACK_ENV from that module
- I get the error above
Link to minimal reproducible example (optional)
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
The root cause is this shortcut return: https://github.com/snowpackjs/snowpack/blob/078b776aec957e5cb03452d958b7e56507c11e95/snowpack/src/build/build-import-proxy.ts#L43-L45
Workaround: just put
import.meta.hot;
on top of the fileI have the same issue with TypeScript. Here is a minimal TypeScript example that shows that SNOWPACK_ENV is undefined:
https://github.com/stefanwille/snowpack-env-vars-bug-demo