打字稿ReferenceError:导出未定义 [英] Typescript ReferenceError: exports is not defined

查看:76
本文介绍了打字稿ReferenceError:导出未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试按照官方手册实施模块,出现此错误讯息:

Trying to implement a module following the official handbook, I get this error message:

未捕获的ReferenceError:未定义导出

Uncaught ReferenceError: exports is not defined

在app.js:2

但是我在代码中的任何地方都没有使用名称exports.

But nowhere in my code do I ever use the name exports.

我该如何解决?

let a = 2;
let b:number = 3;

import Person = require ('./mods/module-1');

module-1.t

 export class Person {
  constructor(){
    console.log('Person Class');
  }
}
export default Person;

tsconfig.json

{
   "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "scripts/"
    },
    "exclude": [
        "node_modules"
    ]
}

推荐答案

根据您是否不再针对es5的情况,此答案可能不起作用,我将尝试使答案更完整.

This answer might not work depending if you're not targeting es5 anymore, I'll try to make the answer more complete.

如果未安装CommonJS(定义exports),将该行从您的tsconfig.json:

If CommonJS isn't installed (which defines exports), you have to remove this line from your tsconfig.json:

 "module": "commonjs",

根据评论,仅此一项可能不适用于tsc的更高版本.在这种情况下,您可以安装诸如CommonJS,SystemJS或RequireJS之类的模块加载器,然后进行指定.

As per the comments, this alone may not work with later versions of tsc. If that is the case, you can install a module loader like CommonJS, SystemJS or RequireJS and then specify that.

查看tsc生成的main.js文件.您将在顶部找到它:

Look at your main.js file that tsc generated. You will find this at the very top:

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

它是错误消息的根源,删除"module": "commonjs",后,它将消失.

It is the root of the error message, and after removing "module": "commonjs",, it will vanish.

这篇关于打字稿ReferenceError:导出未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆