saltyshiomix / nextron

⚡ Next.js + Electron ⚡
https://npm.im/nextron
MIT License
3.69k stars 215 forks source link

I have a nextron with sequelize project, when I run `npm run dev`, it get cannot find module of sequelize config error. #363

Open liaodalin19903 opened 1 year ago

liaodalin19903 commented 1 year ago

I have a nextron with sequelize project, when I run npm run dev, it get cannot find module of sequelize config error.

A JavaScript error occurred in the main process

Uncaught Exception:
Error: Cannot find module '/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/../config/config.json'
at webpackEmptyContext (/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:29:10)
at ./models/index.js (/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:234:101)
at __webpack_require__ (/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:94223:42)
at ./main/ipc/ipcMainHandlers.ts (/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:187:65)
at __webpack_require__ (/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:94223:42)
at /Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:94311:78
at /Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:94342:3
at Object.<anonymous> (/Users/john/Desktop/Test/electron/nextron/nextron-demo-03-for-sequelize/app/background.js:94345:12)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1175:10)

image

there is the nextron with sequelize demo: https://codesandbox.io/s/slrgv7


the key information: models/index.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (
      file.indexOf('.') !== 0 &&
      file !== basename &&
      file.slice(-3) === '.js' &&
      file.indexOf('.test.js') === -1
    );
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
makeusabrew commented 9 months ago

Check out your electron-builder.yml configuration. If you haven't changed it, you'll notice that the while the files property specifies the root directory of your project, it also then filters it down to package.json and app. Looks like you'll want to add config to that list.