Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bfzli/env-parser
A simple parser to convert environment variables to object 🛠️
https://github.com/bfzli/env-parser
env env-parser parser
Last synced: 4 months ago
JSON representation
A simple parser to convert environment variables to object 🛠️
- Host: GitHub
- URL: https://github.com/bfzli/env-parser
- Owner: bfzli
- Created: 2024-07-15T21:40:36.000Z (7 months ago)
- Default Branch: Production
- Last Pushed: 2024-07-16T01:15:08.000Z (7 months ago)
- Last Synced: 2024-09-18T13:19:20.656Z (5 months ago)
- Topics: env, env-parser, parser
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@minilibs/env-parser
- Size: 13.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# **Env Parser 🛠️**
Parse your variables into objects easily, or use it to build something greater with them. Below is a simple explanation of how to use it. It runs smoothly on both the client and the server without conflicts.
When you provide `envPath` (the path to your .env file) as a prop, the function is called on the server side. Alternatively, if you provide `envContent` (a copy-paste of your env files), it runs on either the client or the backend, depending on your environment.
## **How To Env Parser**
### **Importing the Env Parser**
```ts
import EnvParser from '@minilibs/env-parser'// or
import { EnvParser } from '@minilibs/env-parser'
```### **Using The Import**
```ts
// If you want to run it on the server side, provide envPath. For client-side usage, provide envContent, as files cannot be read directly from the client.const { success, error variables } = await EnvParser({
envContent: '...',
envPath: '.env'
})if (success) {
// Prints the parsed variables; may be empty if none are found.
console.log({ variables })
}else {
// If an error occurs, it will provide details to help diagnose the issue.
console.error({ error })
}```
### Fixing a Known Issue with Next.js
If you are running Next.js on the server and encountering a 'module not found' error after you installed `@minilibs/env-parser`, you can resolve this by adding the following code to your next.config.js file:
```js
const nextConfig = {
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback = {
fs: false,
net: false,
tls: false,
request: false,
readline: false,
stream: false,
}
}return config
}
}module.exports = nextConfig
```