如何在TypeScript中读取节点环境变量?

如果我使用process.env。NODE_ENV我有这个错误:

Property 'NODE_ENV' does not exist on type 'ProcessEnv'

我已经安装了@types/node,但它没有帮助。


当前回答

您可以为此使用类型断言

Sometimes you’ll end up in a situation where you’ll know more about a value than TypeScript does. Usually this will happen when you know the type of some entity could be more specific than its current type. Type assertions are a way to tell the compiler “trust me, I know what I’m doing.” A type assertion is like a type cast in other languages, but performs no special checking or restructuring of data. It has no runtime impact, and is used purely by the compiler. TypeScript assumes that you, the programmer, have performed any special checks that you need.

例子

const nodeEnv: string = (process.env.NODE_ENV as string);
console.log(nodeEnv);

或者,您可能会发现像env-var这样的库更适合这个特定的目的——

在node.js中使用正确类型加载和清除环境变量的解决方案

其他回答

还可以使用类型保护函数。像这样,返回类型是

parameterName is string

如。

function isEnvVarSpecified(envVar: string | undefined): envVar is string {
  if(envVar === undefined || envVar === null) {
    return false;
  }
  if(typeof envVar !== 'string'){
    return false;
  }
  return true;
}

然后你可以调用它作为类型保护:

function myFunc() {
  if(!isEnvVarSpecified(process.env.SOME_ENV_VAR')){
      throw new Error('process.env.SOME_ENV_VAR not found')
  }
  // From this point on the ts compiler won't complain about 
  // process.env.SOME_ENV_VAR being potentially undefined
}

执行typescript最新版本后:

NPM install——save @types/node

你可以使用过程。env直接。

console.log(process.env[“NODE_ENV”])

如果您设置了NODE_ENV,您将看到预期的结果。

在使用process.env之前添加即可。NODE_ENV跟随行:

declare var process : {
  env: {
    NODE_ENV: string
  }
}

1. 创建一个.env文件

# Contents of .env file
AUTHENTICATION_API_URL="http://localhost:4000/login"
GRAPHQL_API_URL="http://localhost:4000/graphql"

2. 将.env文件加载到进程中。Env和dotenv

我们可以利用dotenv来设置特定于环境的过程。env变量。创建一个名为config的文件。t在你的src/目录中,并按如下方式填充:

// Contents of src/config.ts

import {config as configDotenv} from 'dotenv'
import {resolve} from 'path'

switch(process.env.NODE_ENV) {
  case "development":
    console.log("Environment is 'development'")
    configDotenv({
      path: resolve(__dirname, "../.env.development")
    })
    break
  case "test":
    configDotenv({
      path: resolve(__dirname, "../.env.test")
    })
    break
  // Add 'staging' and 'production' cases here as well!
  default:
    throw new Error(`'NODE_ENV' ${process.env.NODE_ENV} is not handled!`)
}

注意:这个文件需要导入到最顶层的文件中,可能是src/index文件。Ts通过import '。/config'(放置在所有其他导入之前)

3.检查ENV变量并定义IProcessEnv

在组合了上面的几个方法之后,我们可以添加一些运行时检查,以确保我们声明的IProcessEnv接口反映了在. ENV中设置的ENV变量。*文件。下面的内容也可以保存在src/config.ts中

// More content in config.ts
const throwIfNot = function<T, K extends keyof T>(obj: Partial<T>, prop: K, msg?: string): T[K] {
  if(obj[prop] === undefined || obj[prop] === null){
    throw new Error(msg || `Environment is missing variable ${prop}`)
  } else {
    return obj[prop] as T[K]
  }
}
// Validate that we have our expected ENV variables defined!
['AUTHENTICATION_API_URL', 'GRAPHQL_API_URL'].forEach(v => {
  throwIfNot(process.env, v)
})

export interface IProcessEnv {
  AUTHENTICATION_API_URL: string
  GRAPHQL_API_URL: string
}

declare global {
  namespace NodeJS {
    interface ProcessEnv extends IProcessEnv { }
  }
}
   

这将为我们提供适当的智能感知/tslint类型检查,以及部署到各种环境时的一些理智。

注意,这也适用于ReactJS应用程序(而不是NodeJS服务器应用程序)。你可以省略步骤(2),因为这是由create-react-app处理的。

重要提示:如果你有一个web应用程序,你正在使用webpack。定义流程。Env在你的窗户上,那么这些就是你要找的类型:

declare namespace process {
    let env: {
        // this is optional, if you want to allow also
        // other values than the ones listed below, they will have type 
        // string | undefined, which is the default
        [key: string]: string
        commit_hash: string
        build_time: string
        stage: string
        version: string
        // ... etc.
    }
}