Uncaught ReferenceError: exports is not defined in filed generated by Typescript

前端 未结 7 2112
暗喜
暗喜 2021-02-01 02:41

I\'m trying to get started with Typescript for Electron development. After wrestling with getting typing for node and jquery, I finally got my .ts file error free.

The p

相关标签:
7条回答
  • 2021-02-01 02:43

    There is an issue with the new version of typescript 2.2.1, try using the older version 2.1.6, that solved the exact same issue which you have for me.

    Version 2.2.1 on compiling adds this line Object.defineProperty(exports, "__esModule", { value: true }); while the older 2.1.6 does not.

    0 讨论(0)
  • 2021-02-01 02:53

    I was having the same issue, I just modified the systemjs.config.js file as mentioned below

    'npm:': '/node_modules/' -- // Its value was just 'node_modules/' and I added '/' in the beginning

    'app': '/src/app' -- // Its value was just 'app' and as my app folder path was different so is changed it accordingly

    loader: '/src/systemjs-angular-loader.js' -- // Its value was just 'systemjs-angular-loader.js' and as its location was different in my project so pointed it to the correct path

    0 讨论(0)
  • 2021-02-01 02:56

    I was also facing the same issue and tried by chagning with different versions of typescript but did not work.

    Finally I got it - There was a "type": "module" and when I removed it - it worked

    0 讨论(0)
  • 2021-02-01 02:58

    I've fixed mine with the following:

    tsconfig.json

    {
        "compilerOptions": {
            "target": "ESNext",
            "module": "CommonJS",
            "lib": [
                "DOM",
                "ES5"
            ],
            "esModuleInterop": true
        }
    }
    

    Adding the esModuleInterop

    Removing the "type": "module" from the package.json

    0 讨论(0)
  • I solved it with a hack in the embedding HTML:

    <script> var exports = {}; </script>
    <script src="index.js"></script>
    

    Basically giving it what it wants, a global exports variable.

    With that my TypeScript (2.3.2) generated file (es6) loads.

    0 讨论(0)
  • 2021-02-01 03:03

    I had the same issue with a js file generated by the Typescript compiler. Same line :

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

    And same error :

    game.js:2 Uncaught ReferenceError: exports is not defined
    

    I was defining a Game class in this file. I solved the issue by adding this at the end of my game.ts file:

    export = Game;
    

    With this, the Typescript compiler replaced:

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

    with:

    module.exports = Game;
    

    No more error for me after this.

    0 讨论(0)
提交回复
热议问题