带有 system.js 模块的 TypeScript 在节点错误中运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32445723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript with system.js module running in node error
提问by Grosmar
I would like to use the system.js module manager with TypeScript. My plan is to run typescript in node
我想在 TypeScript 中使用 system.js 模块管理器。我的计划是在节点中运行打字稿
The problem is that, if i mark my Main.js (generated from Main.ts) file as the entry point, it will crash with this error:
问题是,如果我将 Main.js(从 Main.ts 生成)文件标记为入口点,它将因以下错误而崩溃:
ReferenceError: System is not defined
参考错误:系统未定义
And it's normal, because my generated code start like this: System.register(["./Apple"], function(exports_1) {
这很正常,因为我生成的代码是这样开始的: System.register(["./Apple"], function(exports_1) {
And for sure, there is no System class in node by default. So how to make typescript or node to load the system.js module?
当然,默认情况下节点中没有 System 类。那么如何让 typescript 或 node 加载 system.js 模块呢?
I can create an extra js file to load it with require, but i'm looking for a native solution inside typescript
我可以创建一个额外的 js 文件以使用 require 加载它,但我正在寻找 typescript 中的本机解决方案
Thanks!
谢谢!
回答by gilamran
You should tell the compiler to export your modules into commonjs and not systemjs. Node is using commonjs as its native module system.
您应该告诉编译器将您的模块导出到 commonjs 而不是 systemjs。Node 使用 commonjs 作为其原生模块系统。
回答by Michael Vashchinsky
I might not understand correctly, but have you tried to follow the node.js
section on system.js
GitHub Readme (https://github.com/systemjs/systemjs)?
我可能没有正确理解,但是您是否尝试过遵循GitHub 自述文件 ( https://github.com/systemjs/systemjs)node.js
上的部分?system.js
$ npm install systemjs
$ npm install systemjs
then:
然后:
var System = require('systemjs');
// loads './app.js' from the current directory
System.import('./app').then(function(m) {
console.log(m);
});
Hope that helps.
希望有帮助。
回答by Shruti Srivastava
Are you using tsconfig file? You have add the module:'system' I was facing a similar problem, this fixed it. Also ensure that the system.src.js is at top of the scripts.
你在使用 tsconfig 文件吗?您已经添加了模块:'system' 我遇到了类似的问题,这解决了它。还要确保 system.src.js 位于脚本的顶部。