42

Using TypeScript version 1.7.5.

I am writing a nodeJS program in which I want to read in command line arguments passed by the user (2 integers). Everything works fine in raw JavaScript, but an issue arises with TypeScript.

When

process.argv
is used in the TypeScript file, when it is compiled to JavaScript the compiler errors because it does not recognize the "process" variable.
error TS2304: Cannot find name 'process'

I have tried declaring a new var "process" at the top of the file, but that overrides the native variable and it no longer contains the arguments...

I would like to keep all my code all in TypeScript, only compiling to JavaScript at build time. What is the best workaround for this issue?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Rohit Falor
  • 1,254
  • 2
  • 11
  • 17
  • Have you installed the [typings](https://www.npmjs.com/package/typings) for node? i.e. `typings install --save --ambient node` – McMath Feb 22 '16 at 10:27
  • @McMath I just installed those typings, but they didn't have any effect on the error. Just a note - except for this issue, my TypeScript program compiles and runs perfectly. – Rohit Falor Feb 22 '16 at 10:33
  • I think you have to include them manually at the top of your script `/// ` – martin Feb 22 '16 at 10:34
  • OK, there are some more steps you need to take. I'll expand in an answer. Give me a few minutes. – McMath Feb 22 '16 at 10:34

6 Answers6

52

Update: September 2016

You need to make sure that the type definitions for Node are available. The way to do this depends on which version of TypeScript you are using.

TypeScript 1

Use Typings to install the definitions.

typings install --save --global env~node

Be sure to include typings/index.d.ts in your tsconfig.json file. Either include it in the "files" array:

"files": ["typings/index.d.ts"]

Or ensure it is omitted from the "exclude" array.

TypeScript 2

In TypeScript 2, definitions can be installed via npm under the @types scope.

npm install --save-dev @types/node

Original Answer: February 2016

You have to make sure the appropriate type definitions are available. Use the typings package manager for this. Install the definitions for node as follows:

typings install --save --ambient node

Now, there are a few ways you can make sure the definitions are available to the compiler. The preferred method is to set up your tsconfig file like so:

{
  "exclude": [
    "typings/browser.d.ts",
    "typings/browser",
    "node_modules"
  ]
}

Or, alternatively:

{
  "files": [
    "typings/main.d.ts"
  ]
}

If you are not using a tsconfig file, you can use a reference at the top of your main entry file, like so:

/// <reference path="path/to/typings/main.d.ts" />
McMath
  • 6,862
  • 2
  • 28
  • 33
10

This question is at top of Google results when looking for "Cannot find name 'process'", so I'll add my 2 cents to a not-too-unrelated issue here.

If you need to get rid of the error when using if(process.env.NODE_ENV === "production") to strip some code in production with webpack, just add the following declaration before the line:

declare var process: {
   env: {
       NODE_ENV: string
   }
};
Jan Kalfus
  • 5,422
  • 2
  • 30
  • 38
9

TypeScript 2 - Windows Users

Since this question is at the top of Google for "ts2304 cannot find name 'process'" I want to add some help for Windows users. I was attempting to use environmental variables from a config file and was stuck with this error.

TSError: Unable to compile TypeScript.

Cannot find name 'process'- (2304)

I finally added this to my tsconfig.json:

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ]
  }
}

This was with tsconfig.json and node_modules in the same directory level but for some reason the compiler just was not seeing it. What was strange is this error only occurred for the process.env variables as the rest of my code had no trouble compiling.

Kevin Collins
  • 91
  • 1
  • 2
  • 2
    Thank you very much, this did the trick together with adding node typings via `npm install --save-dev @types/node` – rapasoft Mar 16 '17 at 09:32
6

My complete solution for on OSX brings together some of the above for error TS2304: Cannot find name 'process'. My use case was a partially migrated React project from jsx/js to tsx/ts.

  1. yarn add -D @types/node
  2. Updated tsconfig.json to include "typeRoots": ["./node_modules/@types"]
  3. include import { } from 'node'; at the top of the file with the error

All 3 steps were required for me when using Typescript v2.7.1

Simeon
  • 71
  • 2
  • 6
3

I ran into this issue and had to add the following to the file containing the process call:

import { } from 'node';

At which point it recognized process

Aleksandr Albert
  • 1,777
  • 1
  • 19
  • 26
0

After spending too long googling this error...

If using VS Code, check it's open in the right directory. If not, cd into the correct directory, code . to open a new window, then npm i. That fixed it for me - sometimes the simplest solutions take longest to find.

half of a glazier
  • 1,864
  • 2
  • 15
  • 45