Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 使用TypeScript导入节点模块大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让这个工作,但我似乎找不到解决方案的任何地方在SO。尝试编译此单文件应用程序时:

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:查看示例测试文件https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node-tests.ts

大佬总结

以上是大佬教程为你收集整理的node.js – 使用TypeScript导入节点模块全部内容,希望文章能够帮你解决node.js – 使用TypeScript导入节点模块所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。