Is there a project with TypeScript definitions for "process" variable to use process.env.NODE_ENV? Couldn't find anywhere.
-
Nothing worked for my development environment, so I just used https://www.npmjs.com/package/dotenv package, and now it works. – ZiggarDude May 07 '19 at 17:12
7 Answers
Update for Node 8:
Now env
is declared as ProcessEnv
in DefinitelyTyped.
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.
npm install --save-dev @types/node
Pre Node 8 version:
process env
is declared as any
in DefinitelyTyped node.d.ts.
env: any;

- 12,383
- 6
- 46
- 49
-
3Since node.v8 env is declared as ProcessEnv in DefinitelyTyped node.d.ts – Sandokan El Cojo Aug 17 '17 at 11:42
-
1@Sandokan El Cojo: Nice one. Updated based on your comment. – RationalDev likes GoFundMonica Aug 17 '17 at 20:15
-
For anyone looking: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v8/index.d.ts#L574 – mariusz Dec 18 '18 at 12:17
-
Could you give link to repo with this stuff? I copied but its not work, I got error: `Property 'REDIS_SESSION_SECRET' is missing in type 'ProcessEnv' but required in type 'ProcessEnv'. [2741]` my code is `const env: ProcessEnv = process.env` – Yegor Jan 28 '19 at 02:59
-
Thanks this worked for me: npm install --save-dev @types/node – Shridutt Kothari Jul 29 '19 at 10:09
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:
"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.

- 63,782
- 15
- 129
- 193

- 21,508
- 5
- 57
- 54
-
1Note that this won't play very nice with the `webpack-env` DefinitelyTyped – Dan Jul 12 '16 at 10:37
just add before use process.env.NODE_ENV
declare var process : {
env: {
NODE_ENV: string
}
}

- 1,411
- 13
- 13
-
3this is obsoleted approach, please check new approach (`@types/node`) in other answers – mPrinC Aug 20 '18 at 13:52
To add the node definitions in TypeScript 3+
Use the types from Definitely Typed via npm/yarn:
# Install the latest
npm install --save-dev @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-dev @types/node@^12
yarn add @types/node@^12 --dev
With your types available, the object process.env
should be available in your code.
Extending process.env
You can use declaration merging to add new values to process.env
. Create a new d.ts
file in your project, then add:
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.

- 2,521
- 1
- 29
- 47

- 4,225
- 1
- 26
- 34
declare namespace NodeJS {
interface ProcessEnv {
[key: string]: string | undefined
NODE_ENV?: 'development' | 'production' | 'test'
}
}
put the code above in your file global.d.ts

- 51
- 6
If your are using create-react-app your-app-test --template typescript
then already exist a file to set your environment variables.
file name: react-app-env.d.ts
located at src
folder.
then you can set in that file your custom env
variables with this template:
/// <reference types="react-scripts" />
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 {}

- 2,531
- 2
- 15
- 17
If you get this error in VSCode, maybe you just need to restart it.

- 13,684
- 16
- 77
- 148