Search code examples
javascriptelectronelectron-builderelectron-packager

Why does Electron Application not build sometimes?


I have built an electron app. The folder structure is as follows,

App
  js
    script.js
  css
  db
  node_modules
  views
  package.json
  main.js

I have included the js files in the below form, In html,

<head>
    <script src= "../js/jquery.js"></script>
    <script src= "../js/bootstrap.min.js"></script>
    <link rel="stylesheet" href = "../css/css/bootstrap.min.css"/>
</head>
stuff..............
<script src="../js/script.js"></script>

When I build the package using electron-packager, the build is successful. But when I run the application, the functionalities written in script.js are not working.

Update 1 In my script.js, I have written the following code

var Datastore = require('nedb')
  , db = new Datastore({ filename: 'db/data.db', autoload: true });
const fs = require('fs');
const {dialog} = require('electron').remote;
const XLSX = require('xlsx');

$(document).ready(function(){
    pricing_view();shipping_view();etr_view();cost_view();
})
stuff .................
$("#percentage,#channel_name,#brand").bind('keyup mouseup', function   
() {
    populate();
   });

Update 2: I used devtools to find the error. It's saying nedb module not found.

Everything will work fine when I run using npm start. But after build, functions in script.js not work. How to resolve this issue?


Solution

  • I found the answer after 1 day of trying and reading the documentation of electron-packager. When packaging with electron-packager, change the devDependencies to dependencies in the package.json. That's because electron-packager looks for dependencies instead of devDependencies.

    So, before trying to package the app, either rename the devDependencies to dependencies or npm install package_name --save .

    ** Note: Do not use --save-dev when packaging using electron-packager, as -dev will make the dependencies development dependencies.