未捕获的 ReferenceError:Typescript 生成的字段中未定义导出

新手上路,请多包涵

我正在尝试开始使用 Typescript 进行 Electron 开发。在努力输入节点和 jquery 之后,我终于得到了我的 .ts 文件无错误。

现在的问题是,当我运行我的应用程序时,我收到了这个错误:

 index.js:2 Uncaught ReferenceError: exports is not defined

这些是 index.js 中的前两行:

 "use strict";
Object.defineProperty(exports, "__esModule", { value: true });

我不知道那条线会。 Typescript 在编译时添加了它。如果我删除它,我的应用程序可以正常工作。

我该如何摆脱这个错误?

哦,这是我的 tsconfig,如果相关的话。

 {
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "isolatedModules": false,
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declaration": false,
        "noImplicitAny": false,
        "noImplicitUseStrict": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ],
    "compileOnSave": true,
    "buildOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    }
}

原文由 Blargmode 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 604
2 个回答

新版本的 typescript 2.2.1 存在问题,请尝试使用旧版本 2.1.6,它解决了您遇到的完全相同的问题。

编译时的 2.2.1 版本添加了这一行 Object.defineProperty(exports, "__esModule", { value: true }); 而旧的 2.1.6 没有。

原文由 Patronaut 发布,翻译遵循 CC BY-SA 3.0 许可协议

解决方案

tsconfig.json

 "module": "ESNext",

"target": "ESNext",

索引.html

 <script type="module" src="script.js"></script>

原文由 Matej 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题