使用module = system为节点设置TypeScript不起作用

时间:2015-12-27 02:39:01

标签: node.js typescript systemjs

我尝试使用typescript设置一个restify项目。 经过各种尝试后,我能够使用"模块创建一个工作版本:commonjs"在tsconfig.json

我更喜欢使用系统 - 但我无法使用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']

1 个答案:

答案 0 :(得分:2)

系统使用ES6模块加载器,其中nodejs(据我所知)目前不支持,您的初始用例正好在输出到select *, dbo.DictanceKM(cast(u.latitude as float), @lat, cast(u.longitude as float),@long) as distance from Table u where distance < 10 的位置。如果要在节点内使用ES6样式模块分辨率,则需要告知节点如何加载它,例如:

https://www.npmjs.com/package/es6-module-loader

我在节点中使用typescript,但我使用commonjs,我之前在浏览器中使用过commonjs,当你有可用的ES6模块加载器(例如SystemJs(JSPM))时它可以正常工作。