11

I'm trying to run my simple electron app. I use Typescript as a development language which compiles into JavaScript. When I run the app I get the following error:

ReferenceError: exports is not defined[Learn More]
file:///Users/ahmet/Documents/JumbleUp-Desktop/dist/Login/Login.js:5
exports.__esModule = true;

My login.ts file looks like this

    import firebase from "firebase";

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        location.replace("index.html");
    } else {
        location.replace("login.html");
    }
  });
function login() {
    const userEmail = (document.getElementById("inputEmail") as HTMLInputElement).value;
    const userPassword = (document.getElementById("inputPassword") as HTMLInputElement).value;

    firebase.auth().createUserWithEmailAndPassword(userEmail, userPassword).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // ...

        window.alert("Alert : " + errorMessage);
      });
}

and here my tsconfig file

{
    "compilerOptions": {
      "module": "commonjs",
      "noImplicitAny": true,
      "sourceMap": true,
      "esModuleInterop": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
        "*": ["node_modules/*"]
      }
    },
    "include": [
      "src/**/*"
    ]
  } 
3
  • please show us the compiled login.js Commented Feb 11, 2019 at 7:26
  • 1
    Possible duplicate of Typescript ReferenceError: exports is not defined Commented Feb 11, 2019 at 18:07
  • 3
    none of the solutions of the above mentioned link fixed my problem. Commented Feb 11, 2019 at 18:11

1 Answer 1

22

I've encountered the same problem. For me the problem was not in the way the files are transpiled, but by how they were included in the project in index.html.

Changing:

<script src="./main.js"></script>

to

<script>
   require("./main.js")
</script>

in index.html

solved it for me

Sign up to request clarification or add additional context in comments.

4 Comments

HI @kuba-orlik, unfortunately, after making the change at the index.html, now I am getting require is not defined instead - have you seen this when you made the change? (I know this answer was submitted about 3 years ago so thank you anyway :-) )
No, in my case it just started working. Maybe it's got something to do with the contents of the js file that you require? Have you tried a minimal case where the index.html has only one require and it points to a basic js file with like alert("it works") in it?
Thanks for getting back! It also started working for me as well (probably after I had set nodeIntegration under webPreferences to true when creating the BrowserWindow)
It worked but I had to add 'unsafe-inline' to the Content-Security-Policy. Original: <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'"> Working: <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'"> <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.