简体   繁体   中英

How to set environment variables within ts file on NodeJS?

I am struggling with the following error:

error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.

baseUrl: process.env.TEST

// environment.prod.ts

import 'dotenv/config';

export const environment = {
    production: true,
      baseUrl: process.env.TEST
    };

//.env

TEST=whatever

I've already installed @types/node and dotenv, the problem is the IDE on typescript files is telling me there is something wrong with TEST , what am I missing?

Its not enough to install dotenv package, it's types and use it right after with process.env.TEST

You also have to configure your dotenv package in App.ts or any root file of an application you have

something like this:

dotenv.config({ path: __dirname + `/../.production.env}` })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM