API_URL) is not present on your workstation.
This guide walks through the failure scenario, explains why it happens, and shows how to export pod environment variables with Telepresence into a .env file and consume them locally with dotenv.
Problem overview
- The Kubernetes Deployment sets
API_URLin the container environment to point at the in-cluster inventory service. - When you create a Telepresence intercept and run the application locally, those pod environment variables are not automatically available to the local process.
- The local process then attempts to call the inventory service with an undefined URL and throws
ERR_INVALID_URL.
API_URL is undefined:
apiURL is undefined:
apiURL directly; unless you set apiURL from process.env.API_URL the variable will be undefined and the request URL will be invalid.
Deployment manifest example (shows API_URL set in the pod)
Why it fails when intercepting
- The Deployment configures
API_URLfor the container when running in-cluster. - Telepresence intercepts by stopping the real pod traffic and routing traffic to your workstation. It does not automatically inject the pod’s environment variables into your shell.
- Your local process therefore does not have the same environment (internal DNS names, service ports), causing service calls to fail.
Solution overview
Use Telepresence’s--env-file option to export the pod environment to a file (commonly named .env), then load that file into your local Node.js process with a library such as dotenv. This ensures your local process sees the same values (for example, API_URL) as the in-cluster pod.
Step-by-step reproduction and fix
- Make sure there’s no existing intercept for the workload:
- Create an intercept and ask Telepresence to write the pod’s environment to a file (by convention
.env):
- Telepresence will create the
.envfile in your current directory. It contains all the pod environment variables, for example:
- Update your local Node.js application to load
.envand override any existing environment variables so the local process uses the same values as the pod.
dotenv and reads API_URL from process.env:
.env file created by Telepresence and you run your local server with the code above, requests that require the inventory service should succeed because API_URL is now defined as it was in-cluster.
Node runtime note: The example uses global
fetch — available in Node 18+. If you are on an older Node version, install and import node-fetch (or upgrade Node) before using fetch.Quick reference: Telepresence commands and files
Summary
- The Deployment configured
API_URLin the pod, but when you intercept and run locally you must provide those same environment variables on your workstation. - Telepresence’s
--env-fileoption extracts pod environment variables to a file (commonly.env). - Use a library such as
dotenvto load that file into your local process so the local behavior matches the in-cluster environment.
Add your
.env file to .gitignore and avoid committing it. The .env file may contain internal IPs and service endpoints that should not be checked into source control.Links and references
- Telepresence (official project): https://www.telepresence.io/
- Telepresence docs: https://www.telepresence.io/docs/
- dotenv npm package: https://www.npmjs.com/package/dotenv
- Kubernetes documentation: https://kubernetes.io/docs/