-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add env variable in frontend #1130
Comments
Hi! Here is the relevant information: https://shopify.dev/apps/tools/cli/commands#env |
I follow this instruction and I still couldn't solve it. The issue is a set env variable on Heroku but some how it just cannot be read by the frontend. |
Hey, Same, Unable to add new environment variables on vite. on running the pull command the .env file gets created, but if i add new keys the same are not reflecting when i log to console. |
To add new env variable and use it on the Frontend side you need to register it on
export default defineConfig({
root: dirname(fileURLToPath(import.meta.url)),
plugins: [react()],
define: {
"process.env.SHOPIFY_API_KEY": JSON.stringify(process.env.SHOPIFY_API_KEY),
"process.env.<YOUR_NEW_ENV>": JSON.stringify(process.env.<YOUR_NEW_ENV>) // <-- Adding the new env to be accessible on the FE codes
},
resolve: {
preserveSymlinks: true,
},
server: {
host: "localhost",
port: process.env.FRONTEND_PORT,
hmr: hmrConfig,
proxy: {
"^/(\\?.*)?$": proxyOptions,
"^/api(/|(\\?.*)?$)": proxyOptions,
},
},
});
import { useState } from "react";
...
export default function Home () {
...
return (
...
<Link url={`${process.env.YOUR_NEW_ENV}`}></Link>
...
);
} |
It works on production mode. However, when I run my app with "shopify app dev" command for development mode, my app runs and skip the vite.config.js file. So I cant using vite to define new env variable and use it on the frontend. How can I do it on dev mode? |
@Mzn98 That should work for prod and dev environments. Can you show me how you configured the |
I have the same issue on dev mode. |
Guys please, just change the build command in the package.json at the top level to
For some reason this isn't obvious, but that's what you need to do. Follow @LordDashMe to add a new variable first |
where should the .env file be placed? I have registered the variable in |
Following |
You can use env variable in your frontend using vite loadEnv: in your
Btw in local dev mode shopify is adding or overwriting SHOPIFY_API_KEY etc... The .env file is inside /web/frontend |
|
To add more context with my first answer/solution:
|
Thank you! I'll just guess you were talking about |
Yes |
You may also keep using the same const env = loadEnv(mode, path.resolve(process.cwd() + '/..'), ""); |
Try adding: import dotenv from 'dotenv' in vite.config.js |
Issue summary
Write a short description of the issue here ↓
I tried to add analytics to my app. However, I can't access to env variable. How can I do that?
Expected behavior
What do you think should happen?
Actual behavior
What actually happens?
Tip: include an error message (in a
<details></details>
tag) if your issue is related to an errorSteps to reproduce the problem
Reduced test case
The best way to get your bug fixed is to provide a reduced test case.
Specifications
The text was updated successfully, but these errors were encountered: