【发布时间】:2016-04-01 08:02:46
【问题描述】:
我尝试使用 typescript 设置一个 restify 项目。 经过各种尝试,我能够通过在 tsconfig.json 中使用“模块:commonjs”来创建一个工作版本
我更喜欢使用 system - 但我无法使用 systemjs 进行设置
boot.ts
import {AppServer} from './app';
var _appServer = new AppServer();
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
app.ts
/// <reference path="typings/restify/restify.d.ts" />
import {Server, Response, Request, createServer} from 'restify';
export class AppServer {
private server: Server;
constructor() {
this.init();
}
init() {
this.server = createServer();
this.server.get('/hello/:name', this.respond);
this.server.listen(8080, () => {
console.log('%s listening at %s', this.server.name, this.server.url);
});
}
respond(req: Request, res: Response, next: Function) {
res.send('hello ' + req.params.name);
next();
}
}
使用
"module": "system"
在 tsconfig.json 中,我得到以下输出(即使在 boot.ts 中有import System = require('systemjs')):
➜ server git:(master) ✗ npm run server
> server@1.0.0 server /Users/maquh/Development/02_Backgular/server
> node boot.js
/Users/maquh/Development/02_Backgular/server/boot.js:1
(function (exports, require, module, __filename, __dirname) { System.register(['./app'], function(exports_1) {
^
ReferenceError: System is not defined
at Object.<anonymous> (/Users/maquh/Development/02_Backgular/server/boot.js:1:63)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
转译的 boot.js
System.register(['./app'], function(exports_1) {
var app_1;
var _appServer;
return {
setters:[
function (app_1_1) {
app_1 = app_1_1;
}],
execute: function() {
//System.import('./app.ts').
_appServer = new app_1.AppServer();
}
}
});
//# sourceMappingURL=boot.js.map
更新: 我还尝试了 boot.ts
的另一个替代版本var System = require('systemjs');
System.transpiler = 'ts';
System.import('./app.js').then(function(m) {
console.log(m);
}, function(err) {
console.error(err);
});
这会导致以下错误:
[Error: ENOENT: no such file or directory, open '/Users/markusbellgardt/Development/02_Backgular/server/restify']
【问题讨论】:
-
到底是什么问题?你怎么知道你做不到?
-
添加终端输出
-
您正在显示
boot.ts,它似乎没有从boot.js崩溃的行。你能显示boot.js的磁盘内容(编译输出)吗? -
您是否尝试过这样做:
global.System = require('systemjs');in boot.ts? -
我试图这样做(它似乎类似于在 github.com/systemjs/systemjs 上声明的定义 global.ts )但错误仍然相同。我认为主要问题是,tsconfig.json 中的
"module": "system"在我能够声明 System 是什么之前总是添加那些 System.register。
标签: node.js typescript systemjs