env-safeguard 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 +97 -0
- package/index.js +57 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ayantik Sarkar
|
|
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
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# env-safeguard
|
|
2
|
+
|
|
3
|
+
A lightweight Node.js utility to validate environment variables at startup and prevent runtime errors caused by missing or misconfigured `.env` values.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install env-safeguard
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
const { checkEnv } = require("env-safeguard");
|
|
19
|
+
|
|
20
|
+
checkEnv({
|
|
21
|
+
PORT: "number",
|
|
22
|
+
MONGO_URI: "string",
|
|
23
|
+
DEBUG: "boolean"
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
If the environment variables are valid, your application will continue running normally.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Example `.env`
|
|
32
|
+
|
|
33
|
+
```env
|
|
34
|
+
PORT=3000
|
|
35
|
+
MONGO_URI=mongodb://localhost:27017/mydb
|
|
36
|
+
DEBUG=true
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Example Error Output
|
|
42
|
+
|
|
43
|
+
❌ Environment validation failed
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
Missing variables:
|
|
47
|
+
- JWT_SECRET
|
|
48
|
+
|
|
49
|
+
Invalid types:
|
|
50
|
+
- PORT must be a number`
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Supported Types
|
|
57
|
+
|
|
58
|
+
| Type | Description |
|
|
59
|
+
|-----|-------------|
|
|
60
|
+
| string | Any text value |
|
|
61
|
+
| number | Numeric value |
|
|
62
|
+
| boolean | Must be `true` or `false` |
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
This project is licensed under the **MIT License**.
|
|
69
|
+
You are free to use, modify, and distribute this software with proper attribution.
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Author
|
|
74
|
+
|
|
75
|
+
**Ayantik Sarkar**
|
|
76
|
+
|
|
77
|
+
- GitHub: https://github.com/ayantik2006
|
|
78
|
+
- LinkedIn: https://www.linkedin.com/in/ayantiksarkar
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Acknowledgements
|
|
83
|
+
|
|
84
|
+
Thanks to the open-source community for building tools that make Node.js development easier.
|
|
85
|
+
|
|
86
|
+
Special thanks to developers who rely on environment-based configuration and inspired the idea behind **env-safeguard**.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Contributing
|
|
91
|
+
|
|
92
|
+
Contributions, issues, and feature requests are welcome.
|
|
93
|
+
Feel free to open a pull request or create an issue.
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
⭐ If you find this package helpful, consider giving it a star on GitHub!
|
package/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const dotenv = require("dotenv");
|
|
2
|
+
dotenv.config();
|
|
3
|
+
|
|
4
|
+
function checkEnv(envs) {
|
|
5
|
+
const missingVars = [];
|
|
6
|
+
const mismatchedDataTypes = [];
|
|
7
|
+
|
|
8
|
+
for (let env in envs) {
|
|
9
|
+
if (!process.env[env]) {
|
|
10
|
+
missingVars.push(env);
|
|
11
|
+
} else {
|
|
12
|
+
const value = process.env[env];
|
|
13
|
+
|
|
14
|
+
switch (envs[env]) {
|
|
15
|
+
case "boolean":
|
|
16
|
+
if (!(value === "true" || value === "false"))
|
|
17
|
+
mismatchedDataTypes.push(env + " must be boolean");
|
|
18
|
+
break;
|
|
19
|
+
case "number":
|
|
20
|
+
if (isNaN(Number(value)))
|
|
21
|
+
mismatchedDataTypes.push(env + " must be number");
|
|
22
|
+
break;
|
|
23
|
+
case "string":
|
|
24
|
+
if (!isNaN(Number(value)))
|
|
25
|
+
mismatchedDataTypes.push(env + " must be string");
|
|
26
|
+
break;
|
|
27
|
+
default:
|
|
28
|
+
mismatchedDataTypes.push(env + " has invalid data type");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (missingVars.length !== 0 || mismatchedDataTypes.length !== 0) {
|
|
34
|
+
console.log("\n❌ Environment validation failed");
|
|
35
|
+
console.log("--------------------------------");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (missingVars.length !== 0) {
|
|
39
|
+
console.log("Missing variables:");
|
|
40
|
+
for (let vars of missingVars) {
|
|
41
|
+
console.log(" • " + vars);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
console.log("");
|
|
46
|
+
|
|
47
|
+
if (mismatchedDataTypes.length !== 0) {
|
|
48
|
+
console.log("Mismatched variables:");
|
|
49
|
+
for (let vars of mismatchedDataTypes) {
|
|
50
|
+
console.log(" • " + vars);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
console.log("");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = { checkEnv };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "env-safeguard",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight utility to validate environment variables and prevent runtime errors caused by missing or misconfigured .env values.",
|
|
5
|
+
"author": "Ayantik Sarkar",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/ayantik2006/env-safeguard.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"env",
|
|
15
|
+
"environment",
|
|
16
|
+
"dotenv",
|
|
17
|
+
"validation",
|
|
18
|
+
"nodejs",
|
|
19
|
+
"config",
|
|
20
|
+
"env-validator",
|
|
21
|
+
"env-safeguard"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "echo \"No tests yet\""
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"dotenv": "^17.3.1"
|
|
28
|
+
}
|
|
29
|
+
}
|