typescript 如何在节点应用程序中使用@types/node

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39540163/
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-10-21 03:51:15  来源:igfitidea点击:

how to use @types/node in node application

node.jstypescriptnpmdefinitelytyped

提问by Pratik Gaikwad

I am working in VSCode on Ubuntu 16.04. I've created node project using below commads:

我在 Ubuntu 16.04 上使用 VSCode。我使用以下命令创建了节点项目:

npm init
tsc --init

I've created a new file called index.ts. I'm trying to use fs and readlingto read file contents. but when I am writing below lines of code at the top of index.d.ts:

我创建了一个名为index.ts的新文件。我正在尝试使用fs 和 readling来读取文件内容。但是当我在index.d.ts顶部编写以下代码行时

import fs = require('fs');
import readline =  require('readline');

I'm getting below error: can not find module 'fs'and can not find module 'readline'

我收到以下错误: 找不到模块“fs”并且找不到模块“readline”

even processis not found. I've installed typings of node from hereusing below command:

甚至找不到进程。我已经使用以下命令从这里安装了节点类型:

sudo npm install @types/node -global --save

Can anyone please help me how to resolve this error?

任何人都可以帮我解决这个错误吗?

采纳答案by Burt_Harris

For TypeScript1.8, it might be better to typingsto install the node types. For detail see the quickstart at: https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html.

对于TypeScript1.8,最好typings安装节点类型。有关详细信息,请参阅快速入门:https: //basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

回答by jfmercer

Since TypeScript 2.x, all typings are installed using npmlike this: npm install @types/node.

由于打字稿2.X,所有分型使用的是安装npm这样的:npm install @types/node

回答by Victor Balbo

For what i know you have two options here:

据我所知,您在这里有两个选择:

  1. (Recommended) Install devDepencencie npm install @types/node --save-dev, which will add the type module for http.
  2. Create a index.d.tsfile declaring a definition for http module, like: declare module 'http. This method will not enable auto-complete for http methods
  1. (推荐)安装 devDepencencie npm install @types/node --save-dev,它将为 http 添加类型模块。
  2. 创建一个index.d.ts文件来声明 http 模块的定义,例如: declare module 'http. 此方法不会为 http 方法启用自动完成