用TypeScript导入节点模块
我试图让这个工作,但我似乎无法find任何地方的解决scheme。 当试图编译这个单一文件的应用程序:
import http = require('http') http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');
使用命令“tsc app.ts –module'commonjs'”我得到以下错误(不使用–module标志给我一个额外的错误告诉我,我需要它来编译外部模块):
error TS2071: Unable to resolve external module '"http"'. error TS2072: Module cannot be aliased to a non-module type.
TypeScript需要知道http
是否存在。 遵循这两个步骤
- 从这里下载
node.d.ts
文件: https : //github.com/borisyankov/DefinitelyTyped/tree/master/node -
在文件顶部添加:
/// <reference path="node.d.ts" />
PS:查看示例testing文件: https : //github.com/borisyankov/DefinitelyTyped/blob/master/node/node-tests.ts
我发现我有我的tsconfig.json文件中noResolve
设置为true
。 这引起了我对TypeScript文件顶部的.d.ts文件的引用错误。
不应该是这样的
/// <reference path="node.d.ts" /> import http = module('http')
我的意思是,你不应该使用module
而不是require
?
- 通过环境TypeScript公开一个Node.js库
- 在调用AppComponent.ts之前,Angular 2的起点是什么?
- 节点/angular2:用户login后更新导航栏和显示侧栏
- 在打字稿中使用指挥官
- NodeJS,TypeScript和typescript集合
- 为什么TypeScript找不到名字“Express”?
- Typescript模块导入configuration文件
- 如何获取VS Code intellisense来search多个node_modules文件夹中的打字稿
- 如何find在Node + Typescript + VSCodeasynchronous函数调用缺less的等待?