用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
?
- 如何在Angular2中使用'crypto'模块?
- 是否可以在同一个Typescript项目中混合使用AMD和CommonJS模块
- 如何为具有构造函数的复杂外部commonjs模块(如imap)编写TypeScript声明文件?
- Babel + Typescript + NPM模块+ Webpack
- 当使用angular version 2.4.9,typescript&webpack version 2.2.1时,属性'map'在types'Observable <Response>'上不存在
- 在angular2组件中需要节点模块
- Nodejsstream暂停(非pipe理)和恢复(pipe道)中间pipe道
- 如何在npm本地安装和运行Typescript?
- Webstorm导入Angular 2的url问题