简体   繁体   中英

Typescript - ReferenceError: exports is not defined

In my console I keep getting ReferenceError: exports is not defined as an error.

Code:

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"]
    },
};

What am I doing wrong here?

EDIT : I have edited my question with webpack.config.js and tsconfig.json . It might also be worthy to notice that I'm viewing the files directly from Webstorm in my Chrome browser. Could that be an issue?

Thank you.

It may be related to your Typescript config options if you're exporting CommonJS modules. You can fix this by running your bundle through a command line utility called Browserify. See http://thejsguy.com/javascript/browserify/2015/01/05/browserify-getting-started.html

Try to set "allowJs": false

Or to exclude the webpack config file from the typescript compiler using exclude https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM