最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript - ReferenceError: exports is not defined - Stack Overflow

programmeradmin1浏览0评论

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:

{
  "pilerOptions": {
    "outDir": "./build/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "monjs",
    "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.

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:

{
  "pilerOptions": {
    "outDir": "./build/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "monjs",
    "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.

Share Improve this question edited May 11, 2017 at 20:04 tholo asked May 11, 2017 at 18:52 tholotholo 5413 gold badges8 silver badges20 bronze badges 3
  • Is this in Node.js? – EvSunWoodard Commented May 11, 2017 at 19:12
  • I'm viewing from Webstorm directly. Please check my edits. – tholo Commented May 11, 2017 at 19:16
  • I have had the same problem and tried to fix it for couple of minutes with no effect and the problem was that I had my output file in root directory but webpack was creating file in dist/ directory I was using the old one in root directory. :facepalm: – jcubic Commented Jul 12, 2018 at 18:57
Add a ment  | 

2 Answers 2

Reset to default 1

Try to set "allowJs": false

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

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 mand line utility called Browserify. See http://thejsguy./javascript/browserify/2015/01/05/browserify-getting-started.html

发布评论

评论列表(0)

  1. 暂无评论