Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

javascript - Read arguments from command line - error TS2304: Cannot find name 'process'

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?

question from:https://stackoverflow.com/questions/35551185/read-arguments-from-command-line-error-ts2304-cannot-find-name-process

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...