https://github.com/minilibs/env-parser
A lightweight .env parser 🛠️
https://github.com/minilibs/env-parser
env env-parser parser variable variable-parser variables
Last synced: 5 months ago
JSON representation
A lightweight .env parser 🛠️
- Host: GitHub
- URL: https://github.com/minilibs/env-parser
- Owner: minilibs
- Created: 2024-07-15T21:40:36.000Z (over 1 year ago)
- Default Branch: Production
- Last Pushed: 2024-07-16T01:15:08.000Z (over 1 year ago)
- Last Synced: 2025-02-02T06:51:10.517Z (12 months ago)
- Topics: env, env-parser, parser, variable, variable-parser, variables
- Language: TypeScript
- Homepage: https://minilibs.com/packages/env-parser
- Size: 13.7 KB
- Stars: 5
- 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
```