Reputation: 49
I am learning environment variable in Nodejs. I followed a tutorial video and wrote the exact same code in VS Code to set PORT value in the terminal. But for some reason, the output of PORT is undefined.
In editor, my code is like
//This is index.js file
const port = process.env.PORT;
app.listen(port, () => {
console.log(`listening to port ${port}...`)
})
In terminal, I typed set PORT=2000
and pressed Enter.
In the next line of terminal, I typed node index.js
.
The output in the terminal is listening to port undefined
.
My OS is Windows. I tried solutions online, such as PORT=2000 node index.js
and SET PORT=2000 node index.js
. But none of them worked.
Upvotes: 0
Views: 591
Reputation: 1228
These are different ways you can set the environment variables in node in different operating systems:
# Windows CMD:
SET PORT=2000
# or
set PORT=2000
# OS X / Linux:
export PORT=2000
# Windows PowerShell:
$env:PORT=2000
Upvotes: 1