Hi Shopify experts. Can someone please help me. I followed the docs https://shopify.dev/apps/getting-started/create and created a node app template. I ran npm run dev and the app works fine on Shopify admin website, but not localhost. Also, i can’t seem to find the .env file in the project no matter how many times i try to recreate the project. When i create a new .env file, i can’t console log any variables from that file.
Maybe it’s a new update that requires no .env file? I’m new so any elaborations would be really appreciated. Thanks
You can create .env file in web directory. While in web directory, also npm install dotenv. Add
import ‘dotenv/config’ to the top of web/index.js. Now you should be able to put and access environment variables, such as DB connection variables, from the .env file.
Shopify app doesn’t use Next.js anymore. So, it is up to you if you want to re-create .env.{environment} paradigm with dotenv . I don’t suggest doing it. It complicates things. I just use one .env file for local development. I don’t commit it to the repo. I use .env.example file to share env stubs with my co-workers. We deploy Shopify app on AWS ECS using Docker. Build time envs, such as SHOPIFY_API_KEY are defined in Dockerfile (comes with the Shopify app) and we set them during building docker image. Runtime envs, such as SHOPIFY_API_SECRET, database connection variables, etc. are defined as ECS task environment variables. They get picked up when the application runs.
When you develop Shopify locally all you need is .env file and shopify.app.toml (for some reason Shopify stores scopes in that file during local development). However, when you deploy your Shopify app to production, you have to decide how to manage env variables.
Shopify also suggests using fly.io or heroku to deploy Shopify app (/web/docs/fly-io.md, /web/docs/heroku.md). You may want to try those services.
But I’m wonder that could we add custom env variable into the .env file? Shopify Cli provide this command to pull the env file “npx shopify app env pull” but it only contains, if we add new variable it won’t be read in web.
SHOPIFY_API_KEY
SHOPIFY_API_SECRET
SCOPES
If we want to add some variable like DB password, DB URL. How to do that? Don’t we still have to install the dotenv in the web directory? Could we use same .env file in the project root?
Yep. Just use .env in web directory and install dotenv in web directory as well. I haven’t really tried using .env in the root. If you look at Dockerfile, it copies web directory for deployment and not the root. KInda made sense to me to have .env in web directory as well.