@pakoamadi/load-env 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/package.json +37 -4
- package/src/index.js +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pako Amadi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,146 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pakoamadi/load-env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@pakoamadi/load-env)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
Tiny, zero-dependency loader that reads a `.env` file and puts its variables into `process.env`.
|
|
7
|
+
|
|
8
|
+
- **Zero dependencies**: only `node:fs` and `node:path`
|
|
9
|
+
- **Tiny**: about 40 lines of code you can read in a minute
|
|
10
|
+
- **Safe**: never throws; a missing file is silently skipped
|
|
11
|
+
- **Familiar syntax**: comments, quotes, `export` prefix, inline comments, CRLF line endings
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @pakoamadi/load-env
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
Call it once, as early as possible in your app's entry point:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const loadEnv = require('@pakoamadi/load-env');
|
|
25
|
+
|
|
26
|
+
loadEnv(); // reads <cwd>/.env
|
|
27
|
+
|
|
28
|
+
console.log(process.env.PORT);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
By default the file is read from `.env` in the current working directory (`process.cwd()`),
|
|
32
|
+
which is usually your project root when you start the app with `node` or `npm start`.
|
|
33
|
+
|
|
34
|
+
### Custom path
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const path = require('node:path');
|
|
38
|
+
const loadEnv = require('@pakoamadi/load-env');
|
|
39
|
+
|
|
40
|
+
// .env next to the current file
|
|
41
|
+
loadEnv(path.join(__dirname, '.env'));
|
|
42
|
+
|
|
43
|
+
// .env inside the src folder
|
|
44
|
+
loadEnv(path.resolve(process.cwd(), 'src', '.env'));
|
|
45
|
+
|
|
46
|
+
// different files per environment
|
|
47
|
+
loadEnv(path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || 'development'}`));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## `.env` syntax
|
|
51
|
+
|
|
52
|
+
```dotenv
|
|
53
|
+
# Comments start with "#"
|
|
54
|
+
PORT=3000
|
|
55
|
+
DB_URI=mongodb://localhost:27017/app
|
|
56
|
+
|
|
57
|
+
# Quotes are optional; use them to keep "#" or leading/trailing spaces
|
|
58
|
+
PASSWORD="pa#ss word"
|
|
59
|
+
PREFIX=' > '
|
|
60
|
+
|
|
61
|
+
export NODE_ENV=production # "export" prefix and inline comments are supported
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
| Line in `.env` | Value in `process.env` | Notes |
|
|
65
|
+
|---------------------------------|------------------------|-----------------------------------------------|
|
|
66
|
+
| `PORT=3000` | `'3000'` | All values are strings |
|
|
67
|
+
| `PORT = 3000` | `'3000'` | Spaces around `=` are trimmed |
|
|
68
|
+
| `URL=https://x.io/?a=1&b=2` | `'https://x.io/?a=1&b=2'` | Only the first `=` separates key and value |
|
|
69
|
+
| `NAME="John Doe"` | `'John Doe'` | Matching `"…"` or `'…'` are removed |
|
|
70
|
+
| `PAD=" hi "` | `' hi '` | Spaces inside quotes are kept |
|
|
71
|
+
| `PASS="abc#123"` | `'abc#123'` | `#` inside quotes is part of the value |
|
|
72
|
+
| `PORT=3000 # http port` | `'3000'` | Inline comment needs a space before `#` |
|
|
73
|
+
| `COLOR=#fff` | `'#fff'` | No space before `#`, so it is not a comment |
|
|
74
|
+
| `BAD="abc'` | `'"abc\''` | Mismatched quotes are left untouched |
|
|
75
|
+
| `export KEY=value` | `'value'` | `export` prefix is ignored |
|
|
76
|
+
| `EMPTY=` | `''` | Empty value |
|
|
77
|
+
| `FLAG` | `''` | Line without `=` sets an empty string |
|
|
78
|
+
| `=value` | *(ignored)* | Lines with an empty key are skipped |
|
|
79
|
+
| `# comment` / empty line | *(ignored)* | |
|
|
80
|
+
|
|
81
|
+
Both `\n` and `\r\n` line endings are supported.
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
### `loadEnv([customPath])`
|
|
86
|
+
|
|
87
|
+
| Parameter | Type | Default | Description |
|
|
88
|
+
|--------------|----------|--------------------------|---------------------------------|
|
|
89
|
+
| `customPath` | `string` | `<cwd>/.env` | Absolute or relative path to the `.env` file. Relative paths are resolved from `process.cwd()`. |
|
|
90
|
+
|
|
91
|
+
Returns `undefined`. Variables are written directly to `process.env`.
|
|
92
|
+
|
|
93
|
+
## Behavior
|
|
94
|
+
|
|
95
|
+
- **Existing variables are overwritten.** Values from the file take precedence over variables already set in the shell or by a previous `loadEnv()` call. Call `loadEnv()` multiple times to layer files; the last one wins.
|
|
96
|
+
- **Missing file is not an error.** If the file does not exist, nothing happens, so it is safe in production where variables come from the environment.
|
|
97
|
+
- **Read errors are logged, not thrown.** For example, if the path points to a directory, the message `.env file loading error: …` is printed with `console.error` and your app keeps running.
|
|
98
|
+
|
|
99
|
+
### Not supported
|
|
100
|
+
|
|
101
|
+
To keep the package tiny, the following are intentionally not supported:
|
|
102
|
+
|
|
103
|
+
- variable expansion (`URL=http://${HOST}:${PORT}`)
|
|
104
|
+
- multi-line values
|
|
105
|
+
- escape sequences such as `\n` inside double quotes
|
|
106
|
+
|
|
107
|
+
If you need these, consider [`dotenv`](https://www.npmjs.com/package/dotenv) with [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand).
|
|
108
|
+
|
|
109
|
+
## Security
|
|
110
|
+
|
|
111
|
+
Never commit your `.env` file. Add it to `.gitignore`:
|
|
112
|
+
|
|
113
|
+
```gitignore
|
|
114
|
+
.env
|
|
115
|
+
.env.*
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Requirements
|
|
119
|
+
|
|
120
|
+
Node.js **16** or newer.
|
|
121
|
+
|
|
122
|
+
## Testing
|
|
123
|
+
|
|
124
|
+
Tests use the built-in `node:test` runner (Node.js 18+):
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
npm test
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Changelog
|
|
131
|
+
|
|
132
|
+
### 1.0.0
|
|
133
|
+
|
|
134
|
+
Initial release.
|
|
135
|
+
|
|
136
|
+
- `loadEnv([customPath])` loads variables from `<cwd>/.env` (or from `customPath`) into `process.env`
|
|
137
|
+
- Supports comments, empty lines, `\n` and `\r\n` line endings
|
|
138
|
+
- Strips matching `"…"` and `'…'` quotes; mismatched quotes are kept as is
|
|
139
|
+
- Supports inline comments (`KEY=value # comment`) and the `export` prefix
|
|
140
|
+
- Keeps `=` characters inside values
|
|
141
|
+
- Never throws: a missing file is skipped, read errors are logged with `console.error`
|
|
142
|
+
- Zero dependencies, Node.js 16+
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
[MIT](./LICENSE) © Pako Amadi
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pakoamadi/load-env",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Tiny zero-dependency loader for .env files into process.env",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"env",
|
|
7
|
+
"dotenv",
|
|
8
|
+
".env",
|
|
9
|
+
"environment",
|
|
10
|
+
"variables",
|
|
11
|
+
"config",
|
|
12
|
+
"zero-dependency"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/PakoAmadi/load-env#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/PakoAmadi/load-env/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/PakoAmadi/load-env.git"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Pako Amadi",
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"main": "./src/index.js",
|
|
26
|
+
"files": [
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=16"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public",
|
|
34
|
+
"registry": "https://registry.npmjs.org/"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"test": "node --test"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
|
|
6
|
+
const parseValue = (value) => {
|
|
7
|
+
// Matching quotes are stripped and their content is kept as is (including "#")
|
|
8
|
+
const quoted = value.match(/^(["'])(.*)\1(?:\s+#.*)?$/);
|
|
9
|
+
if (quoted) return quoted[2];
|
|
10
|
+
|
|
11
|
+
// Unquoted values may have an inline comment after whitespace
|
|
12
|
+
return value.replace(/\s+#.*$/, '');
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const loadEnv = (customPath = null) => {
|
|
16
|
+
try {
|
|
17
|
+
const envPath = customPath || path.resolve(process.cwd(), '.env');
|
|
18
|
+
if (!fs.existsSync(envPath)) return;
|
|
19
|
+
|
|
20
|
+
const envContent = fs.readFileSync(envPath, 'utf-8');
|
|
21
|
+
const lines = envContent.split(/\r?\n/);
|
|
22
|
+
|
|
23
|
+
for (const line of lines) {
|
|
24
|
+
const trimmed = line.trim();
|
|
25
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
26
|
+
|
|
27
|
+
const [rawKey, ...valueParts] = trimmed.replace(/^export\s+/, '').split('=');
|
|
28
|
+
const key = rawKey.trim();
|
|
29
|
+
const value = valueParts.join('=').trim();
|
|
30
|
+
|
|
31
|
+
if (key) {
|
|
32
|
+
process.env[key] = parseValue(value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error('.env file loading error:', e.message);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
module.exports = loadEnv;
|