63

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 problem is now that when I run my app, I get this error:

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

These are the first two lines in index.js:

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

I don't know that that line does. Typescript added it when compiling. My app works fine if I remove it.

How do I get rid of this error?

Oh and here's my tsconfig, if that's relevant.

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "isolatedModules": false,
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declaration": false,
        "noImplicitAny": false,
        "noImplicitUseStrict": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ],
    "compileOnSave": true,
    "buildOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    }
}
1

12 Answers 12

104

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.

3
  • With this i need to remove : <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'" /> <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'" /> Commented Sep 26, 2020 at 9:39
  • Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-xpAHF/b9vvpXnZU4WcZi0f2tOSSjFy6maGgTvDJZojU='), or a nonce ('nonce-...') is required to enable inline execution.
    – leonheess
    Commented Jun 19, 2022 at 21:31
  • 1
    This is not a great solution for security-aware sites, because you do have to reduce your CSP security specification as several have noted. or mess around with nonces @leonheess you could try adding the nonce to the inline script tag and provide the nonce in your CSP directive.
    – jessewolfe
    Commented Aug 1, 2022 at 1:27
40

I was also facing the same issue and tried changing to different versions of the typescript but did not work.

Finally, I got it - There was a "type": "module" in package.json

, and when I removed it - it worked

4
  • 5
    F&^%$#g lengend! This is the only solution that worked for me across about half a dozen GIthub issues and SO threads. And so concise and to the point. Epic.
    – Ash
    Commented Dec 1, 2020 at 4:06
  • 3
    Where was this spurious "type": "module"? tsconfig.json? Commented Jul 19, 2021 at 11:08
  • 6
    I had type: module in my package.json that was tripping this up. Removing it got it working 👍 (node >16) Commented Aug 30, 2021 at 21:13
  • 2
    This answer needs more upvotes. Even if its not the answer to the question, it is the solution to the question that brings many people to this stackoverflow page.
    – brense
    Commented Feb 12, 2023 at 16:53
14

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.

8
  • 1
    This worked! For anyone using Atom and the Typescript package like me; download typescript 2.1.6 via npm (packagename@version). Then go into the source of the package and replace the typescript folder in node_modules with the one you just downloaded.
    – Blargmode
    Commented Feb 28, 2017 at 15:37
  • 2
    But why the line leads to the erroneous behaviour? Commented Sep 21, 2017 at 10:47
  • 37
    I'm getting this problem in TypeScript 3.4.5. Is the fix still to downgrade to version 2.1.6?
    – stovroz
    Commented May 20, 2019 at 13:33
  • 3
    I'm not even using typescript and I have this issue, should I install typescript 2.1.6 anyways ?
    – Sam Lahm
    Commented May 19, 2021 at 18:54
  • 2
    At this time for sure use the latest TypeScript. I would look for a different solution than using a really old TypeScript version.
    – Patronaut
    Commented May 20, 2021 at 12:31
11

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

8

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
3

Change your tsconfig.json module to es2015 and moduleResolution to node.

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node"
  }
}

Taken from this answer

The exports.__esModule and require('lib') are what happen when we transpile from an ES module to commonjs (with babel or with TypeScript).

2

Solution

tsconfig.json

"module": "ESNext",

"target": "ESNext",

index.html

<script type="module" src="script.js"></script>
0
1

If your code doesn't require export and it doesn't crash application:

You can try to add a global variable to your code:

window.exports = {};
0

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

This might not work for everyone, but my mistake in the ES module was to refer the import with a .ts

We know in ES module, we are supposed to import local modules with:

import { Car } from "../models/car.js";

For TypeScript, I decided to use:

import { Car } from "../models/car.ts";

This made my code break.

Hence, make sure the import is:

import { Car } from "../models/car";

If it does not work, check your package.json, and make sure that it does not have

"type": "module" 

in the surface level. Delete the "type": "module" in your package.json

Then, in my tsconfig.json, make sure you have your targets and modules set to this:

"target" : "es6"
"module" : "CommonJS"

TLDR: The sole problem was my import, but if it doesn't work make sure to check your package.json to have no type of module and your tsconfig.json to have their target and module to be set to es6 and CommonJS respectively.

As if anyone has extra explanations to why the import should be like that feel free to add.

0

Took me almost one hour to solve this: In my typescript file I acidently had this import line on top: import { request } from "express";

I didn't use it, it was the only import. After removing the line, the error was gone. Extra information, my package.json doesn't have 'type' line: "type": "module"

-6

QuickFix

Change "target": "es6" to "target": "es5" in your tsconfig.json.

1
  • 1
    It works it you change the value of "module" to e.g. "es5" Commented Jan 16, 2018 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.