Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
react-inject-env
Advanced tools
react-inject-env
is a tool that allows you to inject your environment variables after building the static files, allowing you to deploy the same build to multiple environments quickly.
npm install react-inject-env --save-dev
yarn add react-inject-env --dev
index.html
<script src='/env.js'></script>
env.js
and copy the following code:export const env = { ...process.env, ...window['env'] }
process.env
with the newly created env
variableimport { env } from './env'
export const App = () => {
return (
<div style={{backgroundColor: env.REACT_APP_COLOR}}>
<span>{env.REACT_APP_MAIN_TEXT}</span>
</div>
)
}
If you are using create-react-app
, the command should be npm run build
or react-scripts build
.
[env variables] npx react-inject-env set
Pass in all your environment variables.
# with a black background
REACT_APP_COLOR=black REACT_APP_MAIN_TEXT="Black Background" npx react-inject-env set
# with a blue background
REACT_APP_COLOR=blue REACT_APP_MAIN_TEXT="Blue Background" npx react-inject-env set
# for windows
set REACT_APP_COLOR=navy&& set REACT_APP_MAIN_TEXT=Navy Background&& npx react-inject-env set
-d / --dir
: The location of your static build folder. Defaults to ./build
-n / --name
: The name of the env file that is outputted. Defaults to env.js
-v / --var
: The variable name in window
object that stores the environment variables. The default is env
(window.env). However if you already have a variable called window.env
, you may rename it to avoid conflicts.
.env
files are supported. react-inject-env
will automatically detect environment variables in your .env
file located in your root folder.
Note: Environment variables passed in through the command line will take precedence over .env
variables.
In step #2, create a file called env.ts
instead of env.js
declare global {
interface Window {
env: any
}
}
// change with your own variables
type EnvType = {
REACT_APP_COLOR: string,
REACT_APP_MAIN_TEXT: string,
REACT_APP_LINK_URL: string,
REACT_APP_LOGO_URL: string
}
export const env: EnvType = { ...process.env, ...window.env }
npx-react-env
works well with both Docker and CI/CD.
FROM node:16.10-slim
COPY . /app
WORKDIR /app
RUN npm install
RUN npm run build
EXPOSE 8080
ENTRYPOINT npx react-inject-env set && npx http-server build
docker build . -t react-inject-env-sample-v2
docker run -p 8080:8080 \
-e REACT_APP_COLOR=yellow \
-e REACT_APP_LOGO_URL=./logo512.png \
-e REACT_APP_MAIN_TEXT="docker text" \
-e REACT_APP_LINK_URL=https://docker.link \
react-inject-env-sample-v2
For instructions on the previous version, you may follow the v1.0 guide here.
A typical CI/CD process usually involves building a base image, followed by injecting variables and deploying it.
Unfortunately React applications does not allow for this workflow as it requires environment variables to be present before building it.
There have been a few workarounds, with the most common solution being to load environment variables from an external source. However this now causes the additional problem that environment variables can only be accessed asynchronously.
react-inject-env
attempts to solve this problem in the simplest, and most straightforward way with the following goals in mind:
react-inject-env
was built with support for both create-react-app
and dotenv
.
However due to the simplicity of it, it should work with almost all scripts and tools.
FAQs
Tool to inject environment variables into your React build
The npm package react-inject-env receives a total of 0 weekly downloads. As such, react-inject-env popularity was classified as not popular.
We found that react-inject-env demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.