我试图让我的第一个Typescript和DefinitelyTyped节点应用程序启动并运行,并遇到一些错误.
当我尝试转换一个简单的ts节点页面时,我收到错误"TS2304:找不到名称'require'".我已经在SO上阅读了其他几个这样的错误,我认为我没有类似的问题.
我在shell提示符下运行命令:tsc movie.server.model.ts.该文件的内容是:
tsc movie.server.model.ts.
Run Code Online (Sandbox Code Playgroud)
var mongoose = require('mongoose')行引发错误
typings/tsd.d.ts文件的内容是:
'use strict';
/// <reference path="typings/tsd.d.ts" />
/* movie.server.model.ts - definition of movie schema */
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var foo = 'test';
Run Code Online (Sandbox Code Playgroud)
.d.ts文件引用放在相应的文件夹中,并通过命令添加到typings/tsd.d.ts:
/// <reference path="node/node.d.ts" />
/// <reference path="requirejs/require.d.ts" />
Run Code Online (Sandbox Code Playgroud)
生成的.js文件似乎工作正常,所以我可以忽略错误.但我很想知道为什么会出现这种错误以及我做错了什么.
我试图让这个工作,但我似乎无法在任何地方找到解决方案.尝试编译此单文件应用程序时:
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/');
Run Code Online (Sandbox Code Playgroud)
使用命令"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.
Run Code Online (Sandbox Code Playgroud)