简体   繁体   English

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

[英]how to use @types/node in node application

I am working in VSCode on Ubuntu 16.04. 我在Ubuntu 16.04上的VSCode中工作。 I've created node project using below commads: 我使用下面的逗号创建了节点项目:

npm init
tsc --init

I've created a new file called index.ts . 我创建了一个名为index.ts的新文件。 I'm trying to use fs and readling to read file contents. 我正在尝试使用fs和readling来读取文件内容。 but when I am writing below lines of code at the top of index.d.ts : 但是当我在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 process is not found. 甚至没有找到过程 I've installed typings of node from here using below command: 我使用下面的命令从这里安装了节点的类型:

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

Can anyone please help me how to resolve this error? 谁能帮助我如何解决这个错误?

从TypeScript 2.x开始,所有类型都使用npm安装,如下所示: npm install @types/node

For TypeScript 1.8, it might be better to typings to install the node types. 对于TypeScript 1.8,安装节点类型的typings可能更好。 For detail see the quickstart at: https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html . 有关详细信息,请参阅快速入门: https//basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM