Typescript - ReferenceError:未定义exports

时间:2017-05-11 18:52:42

标签: javascript typescript webpack

在我的控制台中,我不断将ReferenceError: exports is not defined作为错误。

代码:

app.ts:

import {IDetails} from "./interfaces/IDetails";

class MyClass implements IDetails{
    constructor(){}

    public render (elem: string, text: string) {
        let el: HTMLElement = document.querySelector(elem);
        el.textContent = text;
        el.innerText = text;
    }
}

window.onload = () => {
    let myClass = new MyClass();
    myClass.render('body', 'Hello World!');
};

IDetails.ts:

export interface IDetails {
    elem: string,
    text: string
}

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./build/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es5",
    "removeComments": true,
    "allowJs": true
  }
}

webpack.config.js:

module.exports = {
    entry: './index.ts',
    output: {
        filename: 'bundle.js',
        path: __dirname
    },
    watch: true,
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: "ts-loader",
                options: {
                    transpileOnly: true
                }
            }
        ]
    },
    resolve: {
        extensions: [".tsx", ".ts", ".js"]
    },
};

我在这里做错了什么?

编辑:我已使用webpack.config.jstsconfig.json修改了我的问题。值得注意的是,我在Chrome浏览器中直接从Webstorm查看文件。这可能是一个问题吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您要导出CommonJS模块,它可能与您的Typescript配置选项有关。您可以通过名为Browserify的命令行实用程序运行捆绑包来解决此问题。见http://thejsguy.com/javascript/browserify/2015/01/05/browserify-getting-started.html