Javascript 未捕获的 ReferenceError:在 Typescript 生成的文件中未定义导出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42497479/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:11:57  来源:igfitidea点击:

Uncaught ReferenceError: exports is not defined in filed generated by Typescript

javascripttypescript

提问by Blargmode

I'm trying to get started with Typescript for Electron development. After wrestling with getting typing for node and jquery, I finally got my .ts file error free.

我正在尝试开始使用 Typescript 进行电子开发。在为 node 和 jquery 打字苦苦挣扎之后,我终于摆脱了我的 .ts 文件错误。

The problem is now that when I run my app, I get this error:

现在的问题是,当我运行我的应用程序时,出现此错误:

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

These are the first two lines in index.js:

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

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

I don't know that that line does. Typescript added it when compiling. My app works fine if I remove it.

我不知道那条线可以。打字稿在编译时添加了它。如果我删除它,我的应用程序运行良好。

How do I get rid of this error?

我如何摆脱这个错误?

Oh and here's my tsconfig, if that's relevant.

哦,这是我的 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
    }
}

采纳答案by Patronaut

There is an issue with the new version of typescript 2.2.1, try using the older version 2.1.6, that solved the exact same issue which you have for me.

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

Version 2.2.1 on compiling adds this line Object.defineProperty(exports, "__esModule", { value: true });while the older 2.1.6 does not.

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

回答by Markus Hahn

I solved it with a hack in the embedding HTML:

我通过嵌入 HTML 中的 hack 解决了它:

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

Basically giving it what it wants, a global exportsvariable.

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

With that my TypeScript (2.3.2) generated file (es6) loads.

这样我的 TypeScript (2.3.2) 生成的文件 (es6) 就会加载。

回答by Vikas Chauhan

I was also facing the same issue and tried by chagning with different versions of typescript but did not work.

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

Finally I got it - There was a "type": "module"and when I removed it - it worked

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

回答by mab

I had the same issue with a js file generated by the Typescript compiler. Same line :

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

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

And same error :

同样的错误:

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

I was defining a Game class in this file. I solved the issue by adding this at the end of my game.ts file:

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

export = Game;

With this, the Typescript compiler replaced:

有了这个,Typescript 编译器替换了:

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

with:

和:

module.exports = Game;

No more error for me after this.

在这之后,我再也没有错误了。

回答by Prasanna Miskin

I was having the same issue, I just modified the systemjs.config.jsfile as mentioned below

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

'npm:': '/node_modules/'-- // Its value was just 'node_modules/' and I added '/' in the beginning

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

'app': '/src/app'-- // Its value was just 'app' and as my app folder path was different so is changed it accordingly

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

loader: '/src/systemjs-angular-loader.js'-- // Its value was just 'systemjs-angular-loader.js' and as its location was different in my project so pointed it to the correct path

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

回答by basarat

QuickFix

快速解决

Change "target": "es6"to "target": "es5"in your tsconfig.json.

在 tsconfig.json 中更改"target": "es6""target": "es5"