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.

🐛 BUG: __SNOWPACK_ENV__ is not defined when used in module

See original GitHub issue

Quick 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

  1. Use npx create-snowpack-app to create an app
  2. Create a module ‘constants.js’ in the src directory
  3. Access the SNOWPACK_ENV from that module
  4. I get the error above

Link to minimal reproducible example (optional)

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
ddiachkovcommented, Aug 29, 2021

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 file

image

1reaction
stefanwillecommented, Aug 11, 2021

I 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

🐛 BUG: __SNOWPACK_ENV__ is not defined when ... - GitHub
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...
Read more >
Common Error Details - Snowpack
This page details several common issues and error messages. For further help we have an active GitHub Discussion forumand Discord.
Read more >
Comparing the New Generation of Build Tools - CSS-Tricks
Snowpack is a good choice if you want to double down on unbundled deployment. You may be writing source code with a small...
Read more >
node.js - Error: Cannot find module 'webpack' - Stack Overflow
This fixed it for me: Nodejs cannot find installed module on Windows? "Add an environment variable called NODE_PATH and set it to %USERPROFILE%\Application...
Read more >
referenceerror: module is not defined in es module scope this ...
The error "Module is not defined in ES module scope" occurs when we try to use the module.exports CommonJS syntax in ES modules....
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