0

My index.html has the following scripts.

<script src="js/moment.js"></script>
<script src="js/moment-timezone-with-data.min.js"></script>

When I run the electron app, I get an Uncaught Error: Cannot find module 'moment'. The timezone library seems unable to tell that I have included moment.js.

This does appear to be a problem with Electron, as doing the same thing in a regular HTML file structure leads to everything working fine.

EDIT: I have tried modifying my code in the following manner.

<script src="js/moment.js"></script>
<script onload="window.moment = require(__dirname+'/js/moment.js');" src="js/moment-timezone-with-data.min.js"></script>

This still does not appear to produce any results.

Jason Chen
  • 2,487
  • 5
  • 25
  • 44

3 Answers3

1

This works for me in the current electron project I am working on:

<head>
  <script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

  <script type="application/javascript" src="js/jquery-2.1.3.min.js"></script>
  <script type="application/javascript" src="js/bootstrap.min.js"></script>
</head>

In case you want to write javascript in the html which is not electron related, you can do the following:

<script>
  /* Javascript */
</script>
<script>if (window.module) module = window.module;</script>
<script>
  const ipc = require('electron').ipcRenderer;
  /* Rest of electron related javascript */
</script>
Piero Divasto
  • 1,065
  • 13
  • 21
1

Great! this solves my problem with moment and timezone, working fine in app and in browser

<script>
    if (typeof module === 'object') {window.module = module; module = undefined;}
</script>

my code:

    <!-- inject:js -->
    <script src="lib/index.js"></script>
    <script src="lib/moment/moment-with-locales.min.js"></script>
    <script src="lib/moment-timezone/moment-timezone-with-data.min.js"</script>
    <script src="lib/mdg-foundation/mdg2.js"></script>
    <!-- endinject -->
    <script>if (window.module) module = window.module;</script>
Draken
  • 3,134
  • 13
  • 34
  • 54
danibuiza
  • 77
  • 1
  • 7
0

Huzzah! This problem appears to be solved from this answer.

Electron: jQuery is not defined

Copy-pasted, the answer is as follows:

<!-- Insert this line above script imports  -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

<!-- normal script imports etc  -->
<script src="scripts/jquery.min.js"></script>    
<script src="scripts/vendor.js"></script>    

<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>

Basically, insert the first and last lines before employing script attachments.

Community
  • 1
  • 1
Jason Chen
  • 2,487
  • 5
  • 25
  • 44