TypeScript + Node-Webkit:需要编译器忽略在内部模块中导入外部模块的“错误”

时间:2015-08-10 19:18:39

标签: module typescript node-webkit nw.js

美好的一天。我正在使用Node-Webkit与TypeScript一起开发概念验证跨平台桌面应用程序。 Node-Webkit是带有内置节点的Webkit,它允许我通过import require使用Node apis并使用Webkit for UI(想想Apache Cordova \ Phonegapp),两者都在同一个进程中运行,所以你可以{{1}在您的ViewControllers \ Services中)。 IDE是带有Node Tools 1.1的VS 2015社区

我的问题是,我有一个带有AngularJS控制器的内部模块,就像你在浏览器中所期望的那样,但它导入节点“fs”和“path”模块以在本地文件系统上保存数据。有两种选择:

import

我的快速解决方法是使用/// <reference path="../../typings/node/node.d.ts" /> // Works fine, but TS does not recognize it as module import, // thus, no Intellisense and type checking var path = require("path"); // TS recognizes module import and provides binding, // but gives an error "Import declarations in an // internal module cannot reference an external module" // and WILL NOT COMPILE to JS import path = require("path"); 进行开发,然后在保存之前更改为import

有人可以告诉我如何做以下其中一项:

  1. 取消此特定警告并使文件编译(理想情况下,在文件级别压缩)?
  2. 获取此特定方案的d.ts文件?我可能稍后尝试转换现有的node.d.ts,但也许有人已经完成了它?
  3. 其他一些解决方法?

3 个答案:

答案 0 :(得分:1)

您可以在项目中使用CommonJS模块,并将它们与Browserify结合使用。

使用CommonJS的另一个选择是假的需要函数:

var require = function (oldRequire: (moduleName: string) => any) {
    return (moduleName: string) =>
        exports.sham.modules[moduleName].exports || oldRequire(moduleName);
} (require);

在每个模块的开头写下:

exports.sham("yourModule");

并在引用模块中:

import yourModule = require("yourModule")

请参阅gist

答案 1 :(得分:0)

我会彻底放弃内部模块系统,当我试图让它工作时,它只是痛苦。从那以后,我放弃了他们并且开心。 ;)

如果使用模块,最好向前运行:在任何地方使用ES6 import语句,然后使用commonjs / amd模块(分别用于node / requirejs)编译回ES5,直到我们获得本机ES6导入支持。

答案 2 :(得分:0)

对于在TypeScript 2(2016年9月)发布后查找此内容的任何人,只需安装@types/node,另请参阅this question以获取更多信息。