Javascript 从命令行读取参数 - 错误 TS2304:找不到名称“进程”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35551185/
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
Read arguments from command line - error TS2304: Cannot find name 'process'
提问by Rohit Falor
Using TypeScript version 1.7.5.
使用 TypeScript 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.
我正在编写一个 nodeJS 程序,我想在其中读取用户传递的命令行参数(2 个整数)。在原始 JavaScript 中一切正常,但 TypeScript 出现问题。
When
什么时候
process.argv用于 TypeScript 文件,当它被编译为 JavaScript 时,编译器会出错,因为它无法识别“进程”变量。
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...
我曾尝试在文件顶部声明一个新的 var“process”,但它覆盖了本机变量,它不再包含参数......
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?
我想将所有代码都保留在 TypeScript 中,仅在构建时编译为 JavaScript。此问题的最佳解决方法是什么?
回答by McMath
Update: September 2016
更新:2016 年 9 月
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.
您需要确保 Node 的类型定义可用。执行此操作的方法取决于您使用的 TypeScript 版本。
TypeScript 1
打字稿 1
Use Typingsto install the definitions.
使用Typings安装定义。
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:
请务必包含typings/index.d.ts
在您的tsconfig.json
文件中。要么将其包含在"files"
数组中:
"files": ["typings/index.d.ts"]
Or ensure it is omitted from the "exclude"
array.
或者确保从"exclude"
数组中省略它。
TypeScript 2
打字稿 2
In TypeScript 2, definitions can be installed via npmunder the @types
scope.
在 TypeScript 2 中,定义可以通过@types
作用域下的npm 安装。
npm install --save-dev @types/node
Original Answer: February 2016
原答案:2016 年 2 月
You have to make sure the appropriate type definitions are available. Use the typingspackage 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 filelike so:
现在,有几种方法可以确保定义对编译器可用。首选方法是像这样设置 tsconfig 文件:
{
"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:
如果您不使用 tsconfig 文件,则可以在主条目文件的顶部使用引用,如下所示:
/// <reference path="path/to/typings/main.d.ts" />
回答by Honza Kalfus
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.
在寻找“找不到名称‘进程’”时,这个问题位于谷歌搜索结果的顶部,所以我将在这里添加我的 2 美分到一个不太不相关的问题。
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:
如果你需要在使用if(process.env.NODE_ENV === "production")
webpack 剥离生产中的一些代码时消除错误,只需在该行之前添加以下声明:
declare var process: {
env: {
NODE_ENV: string
}
};
回答by Kevin Collins
TypeScript 2 - Windows Users
TypeScript 2 - Windows 用户
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.
由于这个问题在 Google 的顶部是“ts2304 找不到名称‘进程’”,因此我想为 Windows 用户添加一些帮助。我试图使用配置文件中的环境变量,但遇到了这个错误。
TSError: Unable to compile TypeScript.
Cannot find name 'process'- (2304)
TSError: 无法编译 TypeScript。
找不到名称“进程”- (2304)
I finally added this to my tsconfig.json:
我终于将它添加到我的 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.
这是与 tsconfig.json 和 node_modules 位于同一目录级别,但由于某种原因编译器没有看到它。奇怪的是这个错误只发生在 process.env 变量上,因为我的其余代码编译没有问题。
回答by Simeon
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.
我在 OSX 上的完整解决方案汇集了上述一些内容,error TS2304: Cannot find name 'process'.
我的用例是一个从 jsx/js 部分迁移到 tsx/ts 的 React 项目。
yarn add -D @types/node
- Updated
tsconfig.json
to include"typeRoots": ["./node_modules/@types"]
- include
import { } from 'node';
at the top of the file with the error
yarn add -D @types/node
- 更新
tsconfig.json
以包括"typeRoots": ["./node_modules/@types"]
- 包含
import { } from 'node';
在带有错误的文件顶部
All 3 steps were required for me when using Typescript v2.7.1
使用 Typescript v2.7.1 时,我需要所有 3 个步骤
回答by Aleksandr Albert
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
此时它识别过程
回答by Still_learning
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.
如果使用 VS Code,请检查它是否在正确的目录中打开。如果没有,cd
进入正确的目录,code .
打开一个新窗口,然后npm i
. 这对我来说修复了 - 有时最简单的解决方案需要最长时间才能找到。