如何在TypeScript中读取节点环境变量?
如果我使用process.env。NODE_ENV我有这个错误:
Property 'NODE_ENV' does not exist on type 'ProcessEnv'
我已经安装了@types/node,但它没有帮助。
如何在TypeScript中读取节点环境变量?
如果我使用process.env。NODE_ENV我有这个错误:
Property 'NODE_ENV' does not exist on type 'ProcessEnv'
我已经安装了@types/node,但它没有帮助。
并不能保证在Node进程中可用的环境变量是什么(如果有的话)——NODE_ENV变量只是一个由Express推广的约定,而不是Node本身内置的东西。因此,将它包含在类型定义中实际上没有意义。相反,他们定义过程。这样的环境:
export interface ProcessEnv {
[key: string]: string | undefined
}
这意味着这个过程。Env可以用字符串进行索引,以便返回字符串(如果没有设置变量,则为undefined)。要修复你的错误,你必须使用索引语法:
let env = process.env["NODE_ENV"];
或者,正如jcalz在评论中指出的那样,如果你使用的是TypeScript 2.2或更新版本,你可以使用点语法访问上面定义的可索引类型——在这种情况下,你的代码应该可以正常工作。
在使用process.env之前添加即可。NODE_ENV跟随行:
declare var process : {
env: {
NODE_ENV: string
}
}
执行typescript最新版本后:
NPM install——save @types/node
你可以使用过程。env直接。
console.log(process.env[“NODE_ENV”])
如果您设置了NODE_ENV,您将看到预期的结果。
您可以为此使用类型断言
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中使用正确类型加载和清除环境变量的解决方案
一旦你在你的项目中安装了@types/node,你就可以告诉TypeScript process.env中到底有哪些变量:
environment.d.ts
declare global {
namespace NodeJS {
interface ProcessEnv {
GITHUB_AUTH_TOKEN: string;
NODE_ENV: 'development' | 'production';
PORT?: string;
PWD: string;
}
}
}
// If this file has no import/export statements (i.e. is a script)
// convert it into a module by adding an empty export statement.
export {}
用法:
process.env.GITHUB_AUTH_TOKEN; // $ExpectType string
此方法将为您提供智能感知,并且它还利用了字符串文字类型。
注意:上面的代码片段是模块扩展。包含模块扩展的文件必须是模块(而不是脚本)。模块和脚本的区别在于模块至少有一个导入/导出语句。 为了让TypeScript把你的文件当成一个模块,只需要给它添加一个import语句。它可以是任何东西。甚至export{}也可以。
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处理的。
对于任何来这里寻找Create React App项目答案的人,你的变量名应该以REACT_APP_开头
更多信息请点击:https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
下面是一个简短的函数,它保证会拉动进程。将Env值作为字符串—或以其他方式抛出错误。
对于更强大(但也更大)的东西,这里的其他人建议使用env-var。
/**
* Returns value stored in environment variable with the given `name`.
* Throws Error if no such variable or if variable undefined; thus ensuring type-safety.
* @param name - name of variable to fetch from this process's environment.
*/
export function env(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Missing: process.env['${name}'].`);
}
return value;
}
然后你应该能够编写如下代码:
let currentEnvironment: string;
currentEnvironment = env('NODE_ENV');
还可以使用类型保护函数。像这样,返回类型是
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
}
只需对process.env.YOUR_VAR进行类型转换
例子:
mongoose
.connect(String(process.env.MONGO_URL), {
useNewUrlParser: true,
useFindAndModify: false
})
.then(() => console.log('DB connected'))
.catch((err: any) => console.error(err));
对我有用的是,无论我想在哪里使用过程。我首先导入dotenv并在它上面调用config()。另外,记得附加!最后,确保在.env文件中定义了该属性
从'dotenv'导入dotenv; dotenv.config (); export const YOUR_ATTRIBUTE = process.env.YOUR_ATTRIBUTE!;
补充了之前的回答,并在一段时间后解决了这个问题,甚至安装了@types/node,我找到了这个答案。简而言之,只需要运行一个重载窗口:
"...不过,如果typescript语言服务器仍然使用以前版本的tsconfig,则可能需要重新启动它。为了在VS Code中做到这一点,你可以按Ctrl+Shift+P并重新加载窗口或TypeScript:如果可用,重新启动TS服务器……”
使用节点进程的最佳和最简单的方法。你的typescript项目中的env是首先用tsc编译,然后用node提供你的env var运行编译后的javascript文件。ts是你想要的输出目录也是编译文件的名称,我使用dist作为输出目录和index.js为例):
cd my-typescriptproject
tsc
NODE_ENV=test node ./dist/index.js
通过运行npm i @types/node安装@types/node 在tsconfig中添加"types": ["node"]。json文件在compilerSection部分。
以下是我的envalid解决方案(在Node.js中验证和访问环境变量)
import { str, cleanEnv } from 'envalid'
const env = cleanEnv(process.env, {
clientId: str(),
clientSecret: str(),
})
// and now the env is validated and no longer undefined
const clientId = env.clientId
重要提示:如果你有一个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.
}
}
我知道这将帮助一些人搜索这个,不能找到简单的答案,为什么你的过程。Env变量让你的编译器抱怨:
安装@types /节点:
npm i @types/node
然后当你将env作为字符串包含时,这样做:
process.env.YOUR_ENV ?? ''
双问号允许您检查null/undefined。
我写了一个模块来简化它。它没有依赖关系,所以是相当轻量级的。它也适用于dotenv,您可以传递一个自定义进程。Env到Env .from函数,如果需要的话。
在一些回答中已经提到了它,但这里有一个例子:
使用yarn/npm安装:
npm install env-var --save
然后读取变量:
import * as env from 'env-var'
// Read NODE_ENV and verify that:
// 1) it is set using the required() function
// 2) it is either 'dev' or 'prod'
// 3) throw a runtime exception if conditions #1 or #2 fail
const environment = env.get('NODE_ENV').required().asEnum(['dev', 'prod'])
// Intellisense will suggest 'dev' or 'prod'
if (environment === 'dev') {
console.log('yep, this is dev')
} else {
console.log('looks like this is prod')
}
或另一个:
import { get } from 'env-var'
// Read the GitHub token. It could be undefined
const githubToken = get('GITHUB_TOKEN').asString()
// Read MAX_CONCURRENCY, or default to 5. Throw an error if it's
// not set to a positive integer value
const concurrencyLimit = get('MAX_CONCURRENCY').default(5).asIntPositive()
function callGitApi (token: string, concurrency: number) { /* implementation */ }
// TS Error: Argument of type 'string | undefined' is not assignable to
// parameter of type 'string'.
callGitApi(githubToken, concurrencyLimit)
创建一个类似global.d.ts的文件
declare global {
namespace NodeJS {
interface ProcessEnv {
SECRET: string;
}
}
}
export {};
教程由克里斯蒂安Höller