Skip to content
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

Environment Variables in Hydrogen Development #714

Closed
braginteractive opened this issue Mar 22, 2023 · 3 comments
Closed

Environment Variables in Hydrogen Development #714

braginteractive opened this issue Mar 22, 2023 · 3 comments

Comments

@braginteractive
Copy link

What is the location of your example repository?

No response

Which package or tool is having this issue?

Hydrogen

What version of that package or tool are you using?

2023.1.5

What version of Remix are you using?

1.12.0

Steps to Reproduce

I am trying to load data from a third party API. I have added the URLs, Tokens, and Keys to the .env file.

I am trying to access these env variables in a route loader. In remix applications this works, in a hydrogen application I am not having much success.

The Hydrogen documentation, https://shopify.dev/docs/custom-storefronts/hydrogen/environment-variables, say to refer to the Remix documentation: https://remix.run/docs/en/1.14.3/guides/envvars#server-environment-variables

export const loader = async () => { console.log(process.env.API_URL); };

Expected Behavior

Return the env variables.

Actual Behavior

"ReferenceError: process is not defined"

@jordan-diffagency
Copy link

jordan-diffagency commented Mar 24, 2023

@braginteractive The env variables can be found on the context object which is accessible in remix loaders and actions.
In the sever.js file, the context gets created and passed down in the response.

const handleRequest = createRequestHandler({
        build: remixBuild,
        mode: process.env.NODE_ENV,
        getLoadContext: () => ({session, storefront, env}),
      });

      const response = await handleRequest(request);

@braginteractive
Copy link
Author

braginteractive commented Mar 24, 2023

@jordan-diffagency thanks for this!

What about outside of loaders/actions though?

For example, an api.js file with all the fetch requests:

export async function getCustomers() {
  const res = await fetch(
    process.env.API_URL +
      "/customers/";
    {
      headers,
    }
  );
  const customer = await res.json();
  return customer;
}

Then in then customers route, the loader would be something like:

import {  getCustomers } from "~/lib/api";

export const loader = async ({ request }) => {
  const customer = await getCustomers()
  return json({ customer });
};

I guess I could pass the env as a parameter, but it would be nice if I just had access to the environmental variables in the api.js file. No?

@jordan-diffagency
Copy link

I have been resorting to passing the context as a param though I agree that it would be nice to have access to the env variables directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants