Solved

Purchase Option Extension environment

HunkyBill
Shopify Expert
4851 60 554

Inside the Shopify-rendered code for this extension, how can we tell if we accessing it via a development session or a live production session? Is there a way to know? What is the most common pattern for this? We write one version of the code, and it has to function in both a development sense and a production one.

process and process.env as common ways to handle this do not exist in the Shopify extension, so what do people do?

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Accepted Solution (1)

ElanaK
Shopify Staff
15 4 4

This is an accepted solution.

Since the extension isn't a actually running in a Node environment, `process.env` doesn't quite work out of the box, but our CLI team has used made it possible to use the `process.env.SOME_VAR` syntax in your code to access environment variables from a `.env` file. So, you can't access the whole `process.env` object (it doesn't actually exist in an extension), but if you add  `MY_ENV` or something like that to an `.env` file and then use `process.env.MY_ENV` in your code, you can get your environment (and any other environment variables you set in the same way). Hope that helps!

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 2 (2)

ElanaK
Shopify Staff
15 4 4

This is an accepted solution.

Since the extension isn't a actually running in a Node environment, `process.env` doesn't quite work out of the box, but our CLI team has used made it possible to use the `process.env.SOME_VAR` syntax in your code to access environment variables from a `.env` file. So, you can't access the whole `process.env` object (it doesn't actually exist in an extension), but if you add  `MY_ENV` or something like that to an `.env` file and then use `process.env.MY_ENV` in your code, you can get your environment (and any other environment variables you set in the same way). Hope that helps!

To learn more visit the Shopify Help Center or the Community Blog.

tysonjallen
Shopify Partner
1 0 0

I can't seem to get this to work. `process.env.MY_ENV` doesn't get changed during build/dev with the cli and I get a console error `process not defined` in the browser.

 

Are there any examples I can reference?