Does accessing NODE_ENV make sense in front end code?

前端 未结 4 2051
攒了一身酷
攒了一身酷 2021-02-19 15:30

I have a react/node app that I\'m deploying. One of the components attempts to access NODE_ENV in order to determine which host URL to use (localhost or heroku).

I now r

4条回答
  •  旧巷少年郎
    2021-02-19 16:08

    One of the components attempts to access NODE_ENV in order to determine which host URL to use (localhost or heroku).

    Yup, absolutely!

    There's a difference here between having stuff around at build time, and at runtime. You'll have NODE_ENV around at buildtime - say you run webpack or something to build some JSX, or whatever.

    You won't have NODE_ENV around at runtime, when the user visits your site. There's no concept of environmental variables in that web browsing context.

    What I've done in this case is to programmatically create a file that will conditionally have the web server URL. Or even captures the NODE_ENV and puts the value in the application for later.

    As a practical example: in a React project I was on a couple years ago we autogenerated our index.html file. This was a silly little file, bringing in our

提交回复
热议问题