Search code examples
node.jsreactjsdeploymentpm2

ReactJS development environment works with my API when I use environment variables but my production server can't?


My CRA ReactJS project will work with my .env file and all the stuff I put in the file in development mode aka (my computer), but then when I try to deploy the code to my production server which uses PM2. I have a JS file that deploys the code to the server and builds it on the server, then calls for the app.config.json file that actually runs the web server, but only the NODE_ENV and PORT work, anything I use with REACT_APP_(whatever) doesn't and it just gives me undefined or null (depending on how I call the process.env)

{
  apps : [
    {
      name      : "Project-frontend",
      script    : "npx",
      interpreter: "none",
      args: "serve -s Project-frontend",
      env: {
             NODE_ENV: "production",
             PORT: "3000",
             REACT_APP_TEST: "eevee"
           }
    }
]}

I would like the REACT_APP_TEST to be able to display eevee on the production site.

Also I'm not sure if this would cause the problem but I do have a separate server with Nginx and SSL it runs through


Solution

  • Put your env variable to the .env file before building with react-scripts build (or anyway put the env variable before build).

    serve command only serve a static file, it has no effect to send your env variable after build static file