Does accessing NODE_ENV make sense in front end code?

前端 未结 4 2056
攒了一身酷
攒了一身酷 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:50

    It's a good practice to use NODE_ENV variables for things like API URL.

    If you used create-react-app, all environment variables starting with REACT_APP_ should already be accessible with process.env.REACT_APP_VARIABLE_NAME.
    You can try REACT_APP_TEST=5 npm start then console.log(process.env.REACT_APP_TEST).

    If you are not using create-react-app, you have to import them in your webpack config.

    You can also choose to define the process.env variables for the app through a .env file using dotenv.

提交回复
热议问题