So I just installed dotenv in my project and I am requiring it with this code:
require("dotenv").config();
And then using my variables like so:
app.listen(process.env.PORT, () => {
console.log(`Listening on ${process.env.ENDPOINT}:${process.env.PORT}`);
});
In .env:
ENDPOINT = "127.0.0.1";
PORT = 5000;
Code works just fine if I add the variables as constants within my js file, but when I try to access the variables through process.env I get:
events.js:291 throw er; // Unhandled 'error' event ^
Error: listen EACCES: permission denied 5000; at Server.setupListenHandle [as _listen2] (net.js:1299:21) at listenInCluster (net.js:1364:12) at Server.listen (net.js:1461:5) at Function.listen (C:\Users\Darkbound\Desktop\TouchScreenProject\SmartFactory\server\node_modules\express\lib\application.js:618:24) at Object. (C:\Users\Darkbound\Desktop\TouchScreenProject\SmartFactory\server\main.js:34:5) at Module._compile (internal/modules/cjs/loader.js:1251:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10) at Module.load (internal/modules/cjs/loader.js:1100:32) at Function.Module._load (internal/modules/cjs/loader.js:962:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47 Emitted 'error' event on Server instance at: at emitErrorNT (net.js:1343:8) at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'EACCES',
errno: -4092, syscall: 'listen', address: '5000;', port: -1 }
I have tried using other ports, I get the same error on all ports, I have tried 3000, 3010, 5000 etc.
If the other variable, ENDPOINT
is accesses without problems, so if I do:
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Listening on ${process.env.ENDPOINT}:${PORT}`);
});
This works.
EDIT SOLVED: I found the issue, as usual, right after I ask the question. The issue is that I have ;
at the end of each variable in the .env file. this happened because I copy/pasted the variable from my javascript and I only deleted the const
but forgot about ;
.env
file does not accept;
– Shariati