title: How to access environment variables from Node app client
body: <p>I have a Node app deployed to Elastic Beanstalk. I have javascript that runs client-side to make HTTP requests. The client-side javascript refers to routes that have the port hard-coded in the URI.</p>
  
  <p>Elastic Beanstalk listens on port 8081. In my app.js file, I can write <code>const port = process.env.PORT || 3000</code> and that works just fine (because this runs server-side).</p>
  
  <p>The difficulty I'm having is in my client-side javascript. Suppose I have something like this</p>
  
  <pre><code>fetch('http://localhost:3000/users/login', {...})
  </code></pre>
  
  <p>To run on Elastic Beanstalk, I need to change this to 8081, but if I try to make it dynamic as in app.js, the <code>process</code> variable isn't found. Concretely, if I change the route to</p>
  
  <pre><code>const port = process.env.PORT || 3000 
  fetch('http://localhost:' + port + '/users/login', {...})
  </code></pre>
  
  <p>then I get the following error: <code>ReferenceError: Can't find variable: process</code>.</p>
  
  <p>I think I understand the crux of the problem (the environment variables are only available on the server), but I'm a Node/Express noob, and I'm not sure what the best way is to access the PORT environment variable on the client.</p>