Environment variables compiled incorrectly in a production build when used in combination with a compare.
See original GitHub issueVersion
3.7.0
Environment info
Environment Info:
System:
OS: Windows 10
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Binaries:
Node: 10.11.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.13.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 42.17134.1.0
npmGlobalPackages:
@vue/cli: Not Found
Steps to reproduce
- Add an environment variable for example
VUE_APP_IS_PUBLIC=true
in.env.production
(or development) - Add somewhere in your code
const isPublic = process.env.VUE_APP_IS_PUBLIC === "true";
- Do a production build.
What is expected?
The compiled output should be something like: var s = "true" === "true";
What is actually happening?
What you actually see is: var s = !1;
(Which is false)
A workaround is to change the variable to something like const isPublic = process.env.VUE_APP_IS_PUBLIC
and then check it afterward. It will then compile to: var s = "true";
which is expected.
Could not find any documentation of this so it’s probably a bug. This took me a while to find and happened to gave us some unpredicted behavior since we expected the value to be true.
Environment Info is less relevant because we host it in different environments like Azure and IIS.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Environment Variables in Next.js - Frontend Digest
Here you have the option to define different environment variables depending on whether a production or a preview build is deployed. You can...
Read more >dotenv file is not loading environment variables - Stack Overflow
In the remote case that you arrive till this point, my issue was quite dumber: I wrongly named my env variables with colon...
Read more >Problems with Environment Variables
Environment variable is not set The most likely reason is that your spelling of the variable name is WRONG. To check which environment...
Read more >How to: Use Environment Variables in a Build - MSBuild
Reference environment variables. All environment variables are available to the Microsoft Build Engine (MSBuild) project file as properties.
Read more >Troubleshooting Node.js Deploys - Heroku Dev Center
Check the buildpack · Compare Node and npm Versions · Make sure the lockfile is up to date · Don't check in generated...
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
I’m sorry I think I made a mistake. There is no typescript difference 😅.
However, there is still a significant difference between the following:
var test1 = process.env.VUE_APP_IS_PUBLIC === "true";
var e=!1
var test2 = process.env.VUE_APP_IS_PUBLIC;
var t="true"
Because in the second example, someone could replace the original string value after a build since the original value is used. This is not possible in the first example since the original value is replaced.
Here’s a repo of the above: https://github.com/gilian1993/vue-issue-4015-2
It totally is the case. it works exactly as described.
Your issue is that you try to replace a bit of code that the minifier (not the env variable mechanism) replaced with something smaller (to do its job - minifying your code) at the end of your build process.
You woud have to configure Terser (the minifier) to skip this placeholder (if that’s possible) or that sort of optimization in general.