Next.js: How to detect dev vs production

Created on 6 Mar 2017  路  11Comments  路  Source: vercel/next.js

Forgive me if this is an extremely basic question, but I'm struggling to figure out how to determine if my code is running in dev or production.

I tried adding env vars to my scripts in package.json:

  "scripts": {
    "dev": "ENV=dev next",
    "build": "ENV=production next build",
    "start": "ENV=production next start"
  }

though I don't know how to access these variables. I tried:
`` const ENV = 'undefined' !== typeof window ? window.ENV : process.ENV ```` but this didn't seem to work. These variables don't show up inwindow` object from browser.

From my understanding, window is available on client and process is available on server, but I'm not sure how to set a var on them, or access that var appropriately. Alternatively, is there an existing variable that indicates production vs dev mode?

Thanks!

Most helpful comment

In case someone (else) comes here through Google, check this example...

All 11 comments

I suggest to use NODE_ENV.

Thanks guys.
I think this is answered.

Thank you all!

Hi there,

I don't mean to necro this but I'm having an issue when trying to work this into my project. I only see the root error when I clear my cache which is this image:
screen shot 2017-06-22 at 09 39 47

That follows the documentation on it and makes sense, but the error doesn't to me. I have copied the example @timneutkens links to verbatim into my project, even into the same locations, so I'm confused as to what is missing as when I run the isolated example it seems to work? I started working off the MobX example so maybe it's something in there?

const prod = process.env.NODE_ENV === 'production'

module.exports = {
  'BACKEND_URL': prod ? 'https://api.example.com' : 'https://localhost:8080'
}

Any help is appreciated, thanks,

Steve

P.S. Also note I have updated the .bablerc file as such:

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    "transform-define", "./env-config.js",
    "transform-decorators-legacy"
  ]
}

and installed the transform-define package as well as put /* global BACKEND_URL */ at the top of the file I want to use the global in. Not sure what else I might have missed.

Can't get the example working, and the many layers of interacting libs in the example makes it hard to follow.

  1. I've installed babel-plugin-transform-define
  2. I've added an env-config.js
  3. I've added exporting variables in the env-config
  4. When I run the project, my process.enc.X variables are undefined. This is true both when I run it as DEV and build->start

I'm using a server to render my pages, so not running through "next up" or whatever the command is.

@raelmiu

Have you tried https://github.com/zeit/next.js#exposing-configuration-to-the-server--client-side

How could you use the article mentioned by @johntran to set up two sets of configuration - one for dev and one for prod?

My app communicates with an api, often in the context of a page's getInitialProps() using isomorphic-unfetch. When pages are rendered server-side fetch requires an absolute url, so I've set up my node.config.js like so:

module.exports = {
  publicRuntimeConfig: {
    API_ENDPOINT: 'http://localhost:3000/api'
  }
}

But this endpoint is the development version of the api, I can't have my app contacting the dev api in production! How can I change the API endpoint in node.config.js when the code is running in my production environment?

@onomatopoetry

const prodConfig = {
  publicRuntimeConfig: {
    API_ENDPOINT: 'http://google.com/api'
  }
}

const devConfig = {
  publicRuntimeConfig: {
    API_ENDPOINT: 'http://localhost:3000/api'
  }
}

module.exports = process.env.NODE_ENV === 'production ? prodConfig : devConfig

In case someone (else) comes here through Google, check this example...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timneutkens picture timneutkens  路  3Comments

kenji4569 picture kenji4569  路  3Comments

pie6k picture pie6k  路  3Comments

ghost picture ghost  路  3Comments

sospedra picture sospedra  路  3Comments