
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
A Babel preset let you `import` application configs from **.env** file (zero runtime dependency)
Let you import
environment variables from a .env file in React Native, don't need any native code integration. This is the maintained fork of the abandoned repository.
$ npm install rn-dotenv --save-dev
Add the rn-dotenv
preset to your .babelrc file at the project root.
{
"presets": ["module:metro-react-native-babel-preset", "module:rn-dotenv"]
}
If you haven't got .babelrc set up for React Native, remember to install metro-react-native-babel-preset
first.
$ npm install metro-react-native-babel-preset --save-dev
You may have to reset cache before this works:
$ npm start -- --reset-cache
$ yarn start --reset-cache
$ npx react-native start --reset-cache
Add your app configuration in an .env file.
API_KEY=lorem
ANOTHER_CONFIG=foobar
Now you can import it in your .js file.
import { API_KEY, ANOTHER_CONFIG } from 'rn-dotenv'
ApiClient.init(API_KEY, ANOTHER_CONFIG)
If you're using this in a Typescript react native project, Typescript will complain that the named import does not exist in the package.
Here's a solution to solve the problem.
Steps
env.d.ts
(The filename doesn't matter, as long it ends with .d.ts
)API_KEY
as the environment variable)declare module 'react-native-dotenv' {
/**
* API key
*/
export const API_KEY: string;
}
import { API_KEY } from 'react-native-dotenv'
Solution provided by https://github.com/zetachang/react-native-dotenv/issues/76#issuecomment-585171009
As you can see, it's implemented as a babel plugin. All referenced imported members are replaced as the values specified in the .env file.
The example above will get compiled as below.
ApiClient.init('lorem', 'foobar')
Manually edit the file importing rn-dotenv
by either adding an empty line or whitespace will work.
Yes, simply create a separate .env.production file and the default release process of react-native will pickup the right config.
You can use the Release configuration to launch the Simulator. (Only supported in RN v0.39+)
react-native run-ios --configuration Release
Command⌘
+ M
to launch the developer menu in Android emulator.production
& development
environment configs?Sadly, it's not available so far. One of the workaround is generating .env file before triggering RN's bundle script automatically using either shell script or your own custom build pipeline.
When using a CI tool you will have to write the environment variables to the .env
file. In Github Actions you may for example add a line like echo -e "${{ secrets.DOTENV }}" > .env
.
MIT License, see LICENSE file for detail.
FAQs
A Babel preset let you `import` application configs from **.env** file (zero runtime dependency)
The npm package rn-dotenv receives a total of 1 weekly downloads. As such, rn-dotenv popularity was classified as not popular.
We found that rn-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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.