How to properly use process.env in TypeScript for working with environment variables?
Hi! I'm new to TypeScript and trying to use environment variables in my Node.js application. I wrote simple code:
```typescript
const dbHost = process.env.DB_HOST;
const dbPort = process.env.DB_PORT;
console.log(`Connecting to ${dbHost}:${dbPort}`);
```
But TypeScript complains about `process.env`. Error: "Cannot find name 'process'". I installed `@types/node`, but the problem remains. Also I noticed that `process.env` returns `string | undefined`, and I have to constantly check for `undefined`. This is very annoying, especially when I'm sure the variable is defined.
Another problem: when I use `process.env.PORT` for the server port, I need to convert the string to a number, and if the variable is not set, use a default value. I do it like this:
No answers yet.
Log in to leave an answer