Setting Environment Variables for Node to retrieve

前端 未结 16 2093
-上瘾入骨i
-上瘾入骨i 2020-11-22 15:41

I\'m trying to follow a tutorial and it says:

There are a few ways to load credentials.

  1. Loaded from environment variables,
16条回答
  •  难免孤独
    2020-11-22 16:12

    Step 1: Add your environment variables to their appropriate file. For example, your staging environment could be called .env.staging, which contains the environment variables USER_ID and USER_KEY, specific to your staging environment.

    Step 2: In your package.json file, add the following:

    "scripts": {
      "build": "sh -ac '. ./.env.${REACT_APP_ENV}; react-scripts build'",
      "build:staging": "REACT_APP_ENV=staging npm run build",
      "build:production": "REACT_APP_ENV=production npm run build",
      ...
    }
    

    then call it in your deploy script like this:

    npm run build:staging
    

    Super simple set up and works like a charm!

    Source: https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d

提交回复
热议问题