Does accessing NODE_ENV make sense in front end code?

前端 未结 4 2055
攒了一身酷
攒了一身酷 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 15:58

    You could check NODE_ENV in index.html by using:

    if ("%NODE_ENV%" === "production") {
      // ...do production stuff
    } else {
      // ...do development stuff
    }
    

    When index.html is compiled in a production environment it will look something like this

    if ("production" === "production") {
      // ...do production stuff
    } else {
      // ...do development stuff
    }
    

提交回复
热议问题