electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.46k stars 1.71k forks source link

Error: Cannot find module 'Sequelize' #7354

Closed Yanis02015 closed 1 year ago

Yanis02015 commented 1 year ago

I'm using vitejs and react and sequelize with sqlite3 to make my electron application work

When I'm in the dev environment, everything works fine. When I build with electron-forge the application works very well.

But when I build with electron-builder it builds my app but when I try to open the app I have this error

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'Sequelize'
Require stack:
- /Applications/YourAppName.app/Contents/Resources/app.asar/dist-electron/main.js
- 
at Module._resolveFilename (node:internal/modules/cjs/loader:963:15)
at n._resolveFilename (node:electron/js2c/browser_init:2:109416)
at Module._load (node:internal/modules/cjs/loader:811:27)
at f._load (node:electron/js2c/asar_bundle:2:13328)
at Module.require (node:internal/modules/cjs/loader:1035:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Applications/YourAppName.app/Contents/Resources/app.asar/dist-electron/main.js:78:292411)
at Module._compile (node:internal/modules/cjs/loader:1141:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1196:10)
at Module.load (node:internal/modules/cjs/loader:1011:32)

My package.json

{
  "name": "caisse",
  "private": true,
  "version": "0.0.0",
  "main": "dist-electron/main.js",
  "description": "electron application using Vite, React and Typescript",
  "author": {
    "name": "me",
    "email": "me@me.me",
    "url": "https://yanis.dev"
  },
  "scripts": {
    "dev": "vite dev",
    "build": "tsc && vite build && electron-builder",
    "preview": "vite preview",
  },
  "dependencies": {
    "electron-dl": "^3.5.0",
    "electron-squirrel-startup": "^1.0.0",
    "pg": "^8.8.0",
    "pg-hstore": "^2.3.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "sequelize": "^6.28.0",
    "sqlite3": "^5.1.4",
    "vite-plugin-electron": "^0.11.1"
  },
  "devDependencies": {
    "@types/react": "^18.0.26",
    "@types/react-dom": "^18.0.9",
    "@types/validator": "^13.7.10",
    "@vitejs/plugin-react": "^3.0.0",
    "electron": "^22.0.0",
    "electron-builder": "^23.6.0",
    "typescript": "^4.9.3",
    "vite": "^4.0.0"
  }
}

the sequelize configuration

import path from "path";
const Sequelize = require("Sequelize");

const sequelize = new Sequelize({
  dialect: "sqlite",
  storage: path.resolve(__dirname, "db/test.sqlite"),
});

try {
  sequelize.authenticate();
  console.log("Connection has been established successfully.");
} catch (error) {
  console.error("Unable to connect to the database:", error);
}

(async () => {
  await sequelize
    .sync()
    .then(() => console.log("Tables created successfully"))
    .catch((err: any) => console.log(err));
})();

export { sequelize };
mmaietta commented 1 year ago

I think sqlite has its own .node module, right? If so, that likely means your packaging rules (maybe extraFiles?) for including it in your packaged application. That would also explain why it works in a dev environment, since the file resolution still points at local hard disk versus a packaged application where the path is completely different.

Not sure how electron-forge is automatically packaging it as I'm not familiar with that packager.

Yanis02015 commented 1 year ago

I don't really understand the role of asar:true but when I give it the value false it works, but on the other hand in two clicks I have access to the project file from the .dmg file.

My configuration file: electron-builder.json5

/**
 * @see https://www.electron.build/configuration/configuration
 */
{
  appId: "YourAppID",
  productName: "YourAppName",
  copyright: "Copyright © 2022 ${author}",
  asar: false,
  directories: {
    output: "release/${version}",
    buildResources: "electron/resources",
  },
  files: ["dist-electron", "dist"],
  win: {
    target: [
      {
        target: "nsis",
        arch: ["x64"],
      },
    ],
    artifactName: "${productName}-Windows-${version}-Setup.${ext}",
  },
  nsis: {
    oneClick: false,
    perMachine: false,
    allowToChangeInstallationDirectory: true,
    deleteAppDataOnUninstall: false,
  },
  mac: {
    target: ["dmg"],
    artifactName: "${productName}-Mac-${version}-Installer.${ext}",
  },
  linux: {
    icon: "electron/resources/iconset",
    target: ["AppImage", "deb"],
    artifactName: "${productName}-Linux-${version}.${ext}",
  },
}
mmaietta commented 1 year ago

Would you be willing to create a reproducible example repro of this using electron-quick-start? That'll help me significantly and I can then test it locally, my best guess currently is the extraFiles isn't including .node module.

Re: asar: false, it means that the app is not packaged in the standard app.asar file, so all files are copied over as-is incl. directory structure. For .node files, it should auto-unpack them to app.asar.unpacked but not sure if that currently is occurring. An electron-quick-start repro repo would significantly help