process.env object node js
- Get link
- X
- Other Apps
process.env object
In a React application, process.env
is an object that contains the environment variables set in the environment in which the application is running. This allows you to access sensitive information like API keys and database credentials without hard-coding them in your application code.
Here's an example of how to use process.env
in a React application:
- Create a new environment variable:
To create a new environment variable, you can set it in the terminal or in a .env
file at the root of your project:
# set an environment variable in the terminal export REACT_APP_API_KEY=1234567890abcdef # set an environment variable in a .env file REACT_APP_API_KEY=1234567890abcdef
Note that environment variables used in a React application must start with REACT_APP_
to be recognized.
- Access the environment variable in your application code:
To access the environment variable in your React components, you can use the process.env
object:
import React from 'react'; function MyComponent() { const apiKey = process.env.REACT_APP_API_KEY; // use the apiKey variable in your component return ( <div> <p>API key: {apiKey}</p> </div> ); } export default MyComponent;
Note that environment variables are read-only in a React application, so you cannot modify them at runtime.
By using process.env
to store sensitive information, you can keep your code secure and avoid exposing sensitive information in your application code.
Happy Learning!! Happy Coding!!
- Get link
- X
- Other Apps
Comments