Skip to content

Commit 1229f51

Browse files
committed
refactor: use native Node.js loadEnvFile instead of dotenv
1 parent 0867693 commit 1229f51

File tree

11 files changed

+19
-24
lines changed

11 files changed

+19
-24
lines changed

.env.example

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ ALPHA_VANTAGE_KEY=api-key
2525
DISCORD_CLIENT_ID=discord-client-id/discord-app-id
2626
DISCORD_CLIENT_SECRET=discord-client-secret
2727

28-
# To suppress multiple tip/advertisement lines about the dotenvx service
29-
DOTENV_KEY=""
30-
3128
FACEBOOK_ID=754220301289665
3229
FACEBOOK_SECRET=41860e58c256a3d7ad8267d3c1939a4a
3330
# FB Pixel ID is optional if you are trying to do customer rtracking

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,6 @@ Required to run the project before your modifications
621621
| cheerio | Scrape web pages using jQuery-style syntax. |
622622
| compression | Node.js compression middleware. |
623623
| connect-mongo | MongoDB session store for Express. |
624-
| dotenv | Loads environment variables from .env file. |
625624
| errorhandler | Development-only error handler middleware. |
626625
| express | Node.js web framework. |
627626
| express-rate-limit | Rate limiting middleware for abuse protection. |

app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const compression = require('compression');
77
const session = require('express-session');
88
const errorHandler = require('errorhandler');
99
const lusca = require('lusca');
10-
const dotenv = require('dotenv');
1110
const { MongoStore } = require('connect-mongo');
1211
const mongoose = require('mongoose');
1312
const passport = require('passport');
@@ -17,7 +16,11 @@ const { flash } = require('./config/flash');
1716
/**
1817
* Load environment variables from .env file, where API keys and passwords are configured.
1918
*/
20-
dotenv.config({ path: '.env.example', quiet: true });
19+
try {
20+
process.loadEnvFile('.env.example');
21+
} catch {
22+
console.log('No .env.example file found. This is OK if the required environment variables are already set in your environment.');
23+
}
2124

2225
/**
2326
* Set config values

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"cheerio": "^1.2.0",
5656
"compression": "^1.8.1",
5757
"connect-mongo": "^6.0.0",
58-
"dotenv": "^17.3.1",
5958
"errorhandler": "^1.5.2",
6059
"express": "^5.2.1",
6160
"express-rate-limit": "^8.2.1",
@@ -108,7 +107,6 @@
108107
"supertest": "^7.2.2"
109108
},
110109
"overrides": {
111-
"dotenv": "^17.3.1",
112110
"encoding-sniffer": "github:SeattleDevs/encoding-sniffer",
113111
"fetch-blob": "github:SeattleDevs/fetch-blob",
114112
"formdata-node": "^6.0.3",

test/.env.test

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ ALPHA_VANTAGE_KEY=api-key
1717
DISCORD_CLIENT_ID=discord-client-id/discord-app-id
1818
DISCORD_CLIENT_SECRET=discord-client-secret
1919

20-
# To suppress multiple tip/advertisement lines about the dotenvx service
21-
DOTENV_KEY=""
22-
2320
FACEBOOK_ID=1234567890123456
2421
FACEBOOK_SECRET=test_facebook_secret
2522
FACEBOOK_PIXEL_ID=

test/auth.opt.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('node:path');
22
const { expect } = require('chai');
33
const sinon = require('sinon');
44
const mongoose = require('mongoose');
5-
require('dotenv').config({ path: path.join(__dirname, '.env.test') });
5+
process.loadEnvFile(path.join(__dirname, '.env.test'));
66
const { _saveOAuth2UserTokens } = require('../config/passport');
77
const User = require('../models/User');
88

test/passport.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const sinon = require('sinon');
44
const refresh = require('passport-oauth2-refresh');
55
const mongoose = require('mongoose');
66
const validator = require('validator');
7-
require('dotenv').config({ path: path.join(__dirname, '.env.test') });
7+
process.loadEnvFile(path.join(__dirname, '.env.test'));
88
const passportModule = require('../config/passport');
99
const { isAuthorized, _saveOAuth2UserTokens, _handleAuthLogin } = passportModule;
1010
const User = require('../models/User');

test/playwright.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const fs = require('node:fs');
22
const path = require('node:path');
33
const { defineConfig, devices } = require('@playwright/test');
4-
const dotenv = require('dotenv');
5-
// Preserve any MONGODB_URI that was set before loading dotenv, since it is set to the memory server starter
4+
// Preserve any MONGODB_URI that was set before loading env file, since it is set to the memory server starter
65
const originalMongoUri = process.env.MONGODB_URI;
7-
const result = dotenv.config({ path: path.resolve(__dirname, '.env.test'), quiet: true });
6+
process.loadEnvFile(path.resolve(__dirname, '.env.test'));
87
// If MONGODB_URI was not set by the outer environment (originalMongoUri undefined)
9-
// but exists in the parsed dotenv, remove it so the memory-server starter can create a DB.
10-
if (!originalMongoUri && result && result.parsed && result.parsed.MONGODB_URI) {
8+
// but was loaded from the env file, remove it so the memory-server starter can create a DB.
9+
if (originalMongoUri) {
10+
process.env.MONGODB_URI = originalMongoUri;
11+
} else if (process.env.MONGODB_URI) {
1112
delete process.env.MONGODB_URI;
1213
}
1314

test/token-revocation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('node:path');
22
const { expect } = require('chai');
33
const sinon = require('sinon');
4-
require('dotenv').config({ path: path.join(__dirname, '.env.test') });
4+
process.loadEnvFile(path.join(__dirname, '.env.test'));
55
const { providerRevocationConfig } = require('../config/passport');
66
const { revokeProviderTokens, revokeAllProviderTokens } = require('../config/token-revocation');
77

0 commit comments

Comments
 (0)