【问题标题】:Uncaught ReferenceError: exports is not defined in filed generated by Typescript未捕获的 ReferenceError:Typescript 生成的字段中未定义导出
【发布时间】:2017-07-18 18:12:31
【问题描述】:

我正在尝试开始使用 Typescript 进行 Electron 开发。在与 node 和 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
    }
}

【问题讨论】:

标签: javascript typescript


【解决方案1】:

解决方案

tsconfig.json

"module": "ESNext",

"target": "ESNext",

index.html

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

【讨论】:

    【解决方案2】:

    将您的 tsconfig.json module 更改为 es2015moduleResolution 更改为 node

    {
      "compilerOptions": {
        "target": "es5",
        "module": "es2015",
        "moduleResolution": "node"
      }
    }
    

    Taken from this answer

    exports.__esModulerequire('lib') 是当我们 transpile from an ES module to commonjs(使用 babel 或使用 TypeScript)时发生的。

    【讨论】:

      【解决方案3】:

      我已经修复了以下问题:

      tsconfig.json

      {
          "compilerOptions": {
              "target": "ESNext",
              "module": "CommonJS",
              "lib": [
                  "DOM",
                  "ES5"
              ],
              "esModuleInterop": true
          }
      }
      

      添加 esModuleInterop

      从 package.json 中移除 "type": "module"

      【讨论】:

        【解决方案4】:

        我也遇到了同样的问题,尝试使用不同版本的打字稿,但没有奏效。

        终于我明白了 - 有一个 "type": "module",当我删除它时 - 它起作用了

        【讨论】:

        • F&^%$#g 传奇!这是在大约六个 GIthub 问题和 SO 线程中对我有用的唯一解决方案。如此简洁明了。史诗。
        • 这个虚假的"type": "module"在哪里? tsconfig.json?
        • @NeilBarnwell 是的,你会在 tsconfig.json 中找到它
        • 我有 type: module 在我的package.json 中,这导致了这个问题。删除它可以正常工作?(节点>16)
        【解决方案5】:

        我遇到了同样的问题,我只是修改了 systemjs.config.js 文件,如下所述

        'npm:': '/node_modules/' -- // 它的值只是'node_modules/',我在开头加了'/' p>

        'app': '/src/app' -- // 它的值只是'app',因为我的应用程序文件夹路径不同,所以相应地改变它

        loader: '/src/systemjs-angular-loader.js' -- // 它的值只是 'systemjs-angular-loader.js' 并且它的位置不同在我的项目中,因此将其指向正确的路径

        【讨论】:

          【解决方案6】:

          Typescript 编译器生成的 js 文件也有同样的问题。同一行:

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

          同样的错误:

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

          我在这个文件中定义了一个游戏类。我通过在我的 game.ts 文件末尾添加这个解决了这个问题:

          export = Game;
          

          这样,Typescript 编译器就被替换了:

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

          与:

          module.exports = Game;
          

          在此之后对我来说不再有错误。

          【讨论】:

            【解决方案7】:

            我通过嵌入 HTML 中的 hack 解决了这个问题:

            <script> var exports = {}; </script>
            <script src="index.js"></script>
            

            基本上给它想要的东西,一个全局exports变量。

            我的 TypeScript (2.3.2) 生成的文件 (es6) 加载。

            【讨论】:

            • 有了这个我需要删除:
            【解决方案8】:

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

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

            【讨论】:

            • 这成功了!对于像我这样使用 Atom 和 Typescript 包的人;通过 npm (packagename@version) 下载 typescript 2.1.6。然后进入包源,将node_modules中的typescript文件夹替换为刚刚下载的那个。
            • 但是为什么这条线会导致错误的行为?
            • @МалъСкрылевъ 看看下面 Markus Hahn 的回答:stackoverflow.com/a/43702240/887930
            • 我在 typescript 2.9.2 中遇到了这个问题。现在不是修了吗?
            • 我在 TypeScript 3.4.5 中遇到了这个问题。修复还是降级到 2.1.6 版?
            【解决方案9】:

            快速修复

            在您的 tsconfig.json 中将 "target": "es6" 更改为 "target": "es5"

            【讨论】:

            • "module" 的值更改为例如"es5"
            猜你喜欢
            • 2018-01-21
            • 1970-01-01
            • 1970-01-01
            • 2018-02-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-28
            • 2017-11-01
            相关资源
            最近更新 更多