javascript - typescript - 引用错误 : exports is not defined

标签 javascript typescript webpack

在我的控制台中,我不断收到 ReferenceError: exports is not defined 错误。

代码:

应用.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 查看文件。这会是个问题吗?

谢谢。

最佳答案

尝试设置 "allowJs": false

或者使用 exclude https://www.typescriptlang.org/docs/handbook/tsconfig-json.html 从 typescript 编译器中排除 webpack 配置文件

关于javascript - typescript - 引用错误 : exports is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43923497/

相关文章:

javascript - Babel 7 - 引用错误 : regeneratorRuntime is not defined

reactjs - 让操作停止使用有什么影响?

javascript - fetch 方法中的 http header 接口(interface)

reactjs - 模块联合(意外 token '!==' main.js :619)

javascript - 没有在我的网页中获得倒计时功能

postgresql - 迁移文件中的 NestJS TypeORM 语法错误

javascript - Angular 2 捆绑和缩小

javascript - Angular 模板驱动表单中的模型绑定(bind)不起作用

javascript - CSS - 即使 slider 设置为 100% 宽度,响应式推荐 slider 文本也不会调整大小

javascript - 如何使用 php 和 javascript 从 <select> 中获取单个值并分配给 href 链接