process.env.NODE_ENV 的 TypeScript 定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35666903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:56:55  来源:igfitidea点击:

TypeScript definitions for process.env.NODE_ENV?

typescript

提问by Ross Khanas

Is there a project with TypeScript definitions for "process" variable to use process.env.NODE_ENV? Couldn't find anywhere.

是否有一个带有“流程”变量的 TypeScript 定义的项目以使用 process.env.NODE_ENV?找不到任何地方。

采纳答案by Amid

The definitions for the 'process' variable can be found in default node.js d.ts from definitely typed and added in your typings.json like this:

'process' 变量的定义可以在默认的 node.js d.ts 中找到,来自确定类型并添加到你的 Typings.json 中,如下所示:

"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts"

I do not think there are any definitions for the particular NODE_ENV variable. As it is just a convention (widely used by express link) and node.js itself does not care about that specific environment variable.

我认为特定的 NODE_ENV 变量没有任何定义。因为它只是一个约定(被 express link广泛使用)并且 node.js 本身并不关心那个特定的环境变量。

回答by RationalDev likes GoFundMonica

Update for Node 8:

节点 8 的更新

Now envis declared as ProcessEnvin DefinitelyTyped.

现在env被声明为ProcessEnvDefinitelyTyped

env: ProcessEnv;

export interface ProcessEnv {
    [key: string]: string | undefined;
}

TypeScript 2 supports npm package type definitions for node. It currently uses the DefinitivelyTyped node.d.ts.

TypeScript 2 支持 node 的 npm 包类型定义。它目前使用 DefinitivelyTyped node.d.ts。

npm install --save-dev @types/node

Pre Node 8 version:

节点 8 前版本

process envis declared as anyin DefinitelyTyped node.d.ts.

processenv声明为any绝对类型 node.d.ts

env: any;

回答by haiflive

just add before use process.env.NODE_ENV

使用前添加 process.env.NODE_ENV

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

回答by orta

To add the node definitions in TypeScript 3+

在 TypeScript 3+ 中添加节点定义

Use the types from Definitely Typed via npm/yarn:

通过 npm/yarn 使用来自绝对类型的类型:

# Install the latest
npm install --save @types/node
# or
yarn add @types/node --dev

# To install the right types for your version of node (e.g. 12 here)
npm install --save @types/node@^12
yarn add @types/node@^12 --dev

With your types available, the object process.envshould be available in your code.

如果您的类型可用,则该对象process.env应该在您的代码中可用。

Extending process.env

扩展 process.env

You can use declaration mergingto add new values to process.env. Create a new d.tsfile in your project, then add:

您可以使用声明合并将新值添加到process.env. d.ts在您的项目中创建一个新文件,然后添加:

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      NODE_ENV?: string
    }
  }
}

This will let you write process.env.NODE_ENV. I'd recommend keeping the ? in unless you have validation that it's set somewhere in the library.

这会让你写process.env.NODE_ENV。我建议保留 ? 除非您验证它已设置在库中的某个位置。