
Security News
pnpm 10.16 Adds New Setting for Delayed Dependency Updates
pnpm's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
react-dotenv
Advanced tools
Load environment variables dynamically for your React applications created with CRA (Create-React-App).
Load environment variables dynamically for your React applications created with CRA (Create-React-App).
npm install react-dotenv
Open your project's package.json
file and:
.env
file to your project root (or just load from the system environment variables).react-dotenv
NPM command to your start
, build
(and your serve
commands).react-dotenv.whitelist
property to package.json
to specify which variables you need exposed.package.json:
{
"name": "my-react-app",
"version": "0.1.0",
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-dotenv": "^0.1.0",
"react-scripts": "3.4.3",
"serve": "^11.3.2"
},
"scripts": {
"start": "react-dotenv && react-scripts start", // <-- append command
"build": "react-dotenv && react-scripts build", // <-- append command
"serve": "react-dotenv && serve build", // <-- append command
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [">0.2%", "not dead", "not op_mini all"],
"development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"]
},
// Add the react-dotenv configuration
"react-dotenv": {
"whitelist": ["API_URL"]
}
}
npm start
Now your project have the environment variables loaded globally in the window.env
property.
You can access the environment variables from your code in two ways:
react-dotenv
libraryimport React from "react";
import env from "react-dotenv";
export function MyComponent() {
return <div>{env.API_URL}</div>;
}
window.env
global variableimport React from "react";
export function MyComponent() {
return <div>{window.env.API_URL}</div>;
}
FAQs
Load environment variables dynamically for your React applications created with CRA (Create-React-App).
The npm package react-dotenv receives a total of 5,589 weekly downloads. As such, react-dotenv popularity was classified as popular.
We found that react-dotenv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.