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

Compiled build inlines PORT environment variable #356

Closed
d4rky-pl opened this issue Sep 21, 2017 · 13 comments
Closed

Compiled build inlines PORT environment variable #356

d4rky-pl opened this issue Sep 21, 2017 · 13 comments
Labels

Comments

@d4rky-pl
Copy link
Contributor

Due to how getClientEnvironment is written, PORT environment variable is being inlined into final build instead of being kept as process.env.PORT.

This will cause Razzle to fail launching on Heroku using start:prod

@d4rky-pl
Copy link
Contributor Author

d4rky-pl commented Sep 21, 2017

I've also noticed that the way getClientEnvironment stringified function is written, entire process.env is replaced with inlined object including all environment variables:

// console.log(process.env.PORT) gets compiled to:
console.log(Object({"NODE_ENV":"production","PORT":3000,"VERBOSE":false,"HOST":"localhost","RAZZLE_ASSETS_MANIFEST":"somepath","BUILD_TARGET":"server","RAZZLE_PUBLIC_DIR":"somepath"}).PORT);

This makes it impossible to make the server app configuration dynamic without rebuilding it every time.

@xouabita
Copy link
Contributor

Hi @d4rky-pl,
You can use razzle-heroku :) It extends the default razzle config to make it works on heroku.
See #340 for more infos

@d4rky-pl
Copy link
Contributor Author

@xouabita this is nice, thanks :)

I feel that Razzle should support this by default or have a caveat about process.env and production build somewhere more prominent.

@d4rky-pl
Copy link
Contributor Author

This turns out to be even more problematic than I imagined. Let me explain:

I'm writing a system tests runner (system tests: tests that run in actual browser) and I want to add razzle support from day one. The problem is that because PORT is inlined in resulting build, I have to also inline it when compiling everything before testing.

This may cause unnecessary confusion when someone then tries to use the same build for something else (it's the production build after all, other than PORT there are zero changes) as the inlined PORT in tests is different than the one during "normal" production compilation. I could also drop the build after running tests but it will break the flow where we do system tests before production deployment and then reuse the same compiled code.

@jaredpalmer please reconsider the decision to inline PORT in production build and add one-time lookup (something like const PORT = process.env.PORT at the beginning of server.js) instead. If there's no chance in hell for that, please close this issue and I'll add a disclaimer in my tool :)

@jaredpalmer
Copy link
Owner

@d4rky-pl let's make this right.

what if we blacklisted

  • process.env.RAZZLE_SERVER_ will not get compiled
  • and PORT

I;m sure you probably know this, but reading from process.env. during runtime is incredibly slow and something that should be avoided whenever possible. However, it would be better if razzle was more easily deployable to heroku etc. so yeah. let's figure this out.

@d4rky-pl
Copy link
Contributor Author

d4rky-pl commented Nov 23, 2017

I like this. In case of PORT the performance should not be a problem, this env variable is only being read during the boot time.

As for RAZZLE_SERVER_, sounds like a good workaround for those couple of extra cases, though I'm not sure about the name - it might be confusing and lead to decreased performance when people assume this is how you should set all server-side environment variables (instead of only those that you actually want to override during application boot). I'm afraid I don't have a better option though.

@ramasilveyra
Copy link

ramasilveyra commented Jan 7, 2018

I had the same problem, also happens with env vars not defined at buildtime. This is the workaround:

/* eslint-disable no-param-reassign */
const razzleConfigEnv = require('razzle/config/env');

module.exports = {
  modify: (config, { target, dev }, webpack) => {
    // Fix process.env
    if (target === 'node') {
      config.plugins = config.plugins.filter(plugin => plugin.constructor.name !== 'DefinePlugin');
      const dotenv = razzleConfigEnv.getClientEnv(target, {
        clearConsole: true,
        host: 'localhost',
        port: 3000
      });
      config.plugins.push(
        new webpack.DefinePlugin({
          'process.env': `Object.assign(${JSON.stringify(dotenv.raw)}, process.env)`
        })
      );
    }
    return config;
  }
};

@JacopKane
Copy link

JacopKane commented Feb 16, 2018

This is my workaround for Heroku:

// getPorts.js

// bypass webpack.DefinePlugin
const { env } = require('process')

export const port = () =>
  parseInt(
    env.RAZZLE_PORT ||
      env.PORT ||
      process.env.RAZZLE_PORT ||
      process.env.PORT ||
      3000,
    10,
  )

pcraig3 added a commit to cds-snc/ircc-rescheduler that referenced this issue Aug 8, 2018
Heroku prohibits you from setting a port on startup.
They are going to generate a port for you and then you are going
to listen for it as an env var.

- https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

Unfortunately, Razzle defaults to setting port 3000 if you
don't set one yourself.

- https://github.com/jaredpalmer/razzle#build-time-variables

There's some issues about this on the razzle repo, and ultimately
this is where I found the solution.

Source: jaredpalmer/razzle#356 (comment)
pcraig3 added a commit to cds-snc/ircc-rescheduler that referenced this issue Aug 8, 2018
Heroku prohibits you from setting a port on startup.
They are going to generate a port for you and then you are going
to listen for it as an env var.

- https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

Unfortunately, Razzle defaults to setting port 3000 if you
don't set one yourself.

- https://github.com/jaredpalmer/razzle#build-time-variables

There's some issues about this on the razzle repo, and ultimately
this is where I found the solution.

Source: jaredpalmer/razzle#356 (comment)
pcraig3 added a commit to cds-snc/ircc-rescheduler that referenced this issue Aug 8, 2018
Heroku prohibits you from setting a port on startup.
They are going to generate a port for you and then you are going
to listen for it as an env var.

- https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

Unfortunately, Razzle defaults to setting port 3000 if you
don't set one yourself.

- https://github.com/jaredpalmer/razzle#build-time-variables

There's some issues about this on the razzle repo, and ultimately
this is where I found the solution.

Source: jaredpalmer/razzle#356 (comment)
@stale
Copy link

stale bot commented Aug 15, 2018

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

@stale stale bot added the stale label Aug 15, 2018
@stale
Copy link

stale bot commented Aug 22, 2018

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.

@stale stale bot closed this as completed Aug 22, 2018
dsamojlenko pushed a commit to cds-snc/ircc-rescheduler that referenced this issue Sep 1, 2018
Heroku prohibits you from setting a port on startup.
They are going to generate a port for you and then you are going
to listen for it as an env var.

- https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

Unfortunately, Razzle defaults to setting port 3000 if you
don't set one yourself.

- https://github.com/jaredpalmer/razzle#build-time-variables

There's some issues about this on the razzle repo, and ultimately
this is where I found the solution.

Source: jaredpalmer/razzle#356 (comment)
@ericvicenti
Copy link
Contributor

I found a pretty simple workaround to this issue, if people are still struggling with it

  // This will extract the env during production execution.. PORT will not be inlined during build
  const getEnv = c => process.env[c];
  app.listen(getEnv('PORT'));

@vbutani
Copy link

vbutani commented Jul 21, 2019

I had the same problem, also happens with env vars not defined at buildtime. This is the workaround:

/* eslint-disable no-param-reassign */
const razzleConfigEnv = require('razzle/config/env');

module.exports = {
  modify: (config, { target, dev }, webpack) => {
    // Fix process.env
    if (target === 'node') {
      config.plugins = config.plugins.filter(plugin => plugin.constructor.name !== 'DefinePlugin');
      const dotenv = razzleConfigEnv.getClientEnv(target, {
        clearConsole: true,
        host: 'localhost',
        port: 3000
      });
      config.plugins.push(
        new webpack.DefinePlugin({
          'process.env': `Object.assign(${JSON.stringify(dotenv.raw)}, process.env)`
        })
      );
    }
    return config;
  }
};

I tried this but when I try 'npm run start:prod' on my local, I get an error that assets.json not found. Also, could you please advise, do I need to upload node_modules in build folder as well as I get npm package related errors as well.

@KevinMind
Copy link

KevinMind commented Aug 11, 2020

This is working for me now.

`
appConfig.plugins = appConfig.plugins.filter(plugin => plugin.constructor.name !== 'DefinePlugin');
const dotenv = razzleConfigEnv.getClientEnv(target);

  delete dotenv.raw.PORT;
  config.plugins.push(
    new webpack.EnvironmentPlugin(dotenv.raw)
  );

`

MattTennison added a commit to MattTennison/homepage that referenced this issue Apr 11, 2021
This was preventing me from deploying to Dokku.

Related issue: jaredpalmer/razzle#356
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants