Webpack隔离,sqlite3错误找不到模块sqlite3

时间:2021-11-21 04:13:50

I try to use sequelize and sqlite3 with electron and I have problems when using it with webpack. To test the projects provided in my description do npm install before

我试着用电子的sequelize和sqlite3,在使用webpack时我遇到了一些问题。要测试我描述中提供的项目,请在之前安装npm

Example 1 : I tried two projects one without webpack and it works well : The sqlite3 load correctly and write the data in the database.  https://www.dropbox.com/sh/yag6690rnc5l9y0/AAA0WeKvnHxhxY4qtxhHONWma?dl=0 

示例1:我尝试了两个项目,一个没有webpack,它运行得很好:sqlite3正确地加载并在数据库中写入数据。https://www.dropbox.com/sh/yag6690rnc5l9y0/AAA0WeKvnHxhxY4qtxhHONWma?dl=0

npm start will launch node_modules/.bin/electron . 

It launches the index.html that uses the index.js file:

它推出的指数。使用索引的html。js文件:

Webpack隔离,sqlite3错误找不到模块sqlite3

Example 2: I use webpack to compile the project because I use ES6 syntax (in the provided example the syntax is not ES6, it just to reuse the same file as in the example 1). https://www.dropbox.com/sh/g38305knvrpdala/AAANbVBXlc-scBrzdCQTqqI7a?dl=0

示例2:我使用webpack来编译项目,因为我使用了ES6语法(在提供的示例中,语法不是ES6,它只是重用与示例1相同的文件)

npm start will compile the index.js entry point and output a bundle.js file that is used by the index.html for the electron app.

npm start将编译索引。js入口点并输出一个bundle。用于索引的js文件。电子应用程序的html。

Webpack隔离,sqlite3错误找不到模块sqlite3

When I launch the second example, the index.html that load the bundle.js file compiled with webpack I have the error below:

当我启动第二个例子时,索引。加载包的html。用webpack编译的js文件有以下错误:

Webpack隔离,sqlite3错误找不到模块sqlite3

To see the ERRRRR issss,

为了看到误差,

  • I added a console.log in the file node_modules/sequelize/lib/dialects/sqlite/connection-manager.js line 22 as specified in this link. (Electron and sequelize error: the dialect sqlite is not supported)
  • 我添加了一个控制台。登录文件node_modules/sequelize/lib/方言/sqlite/连接管理器。在此链接中指定的js行22。(电子和顺序错误:不支持方言sqlite)

Unlike in the link provided which is not about using webpack with electron. The link is my Example 1 project for me everything works perfectly on the first try without doing anything more. But I tried every solution provided for my webpack example 2 but nothing works.

不像在提供的链接,不是关于使用webpack与电子。链接是我的示例1项目,对我来说,在第一次尝试时,所有东西都可以完美地工作,而不需要做更多的工作。但是我尝试了webpack示例2中提供的所有解决方案,但是没有一个有效。

I do not understand why, in the basic example when just include the index.js file directly in the html it works well, but when processed by webpack and the included in the same html it doesn’t work ? Am i missing something here ?

我不明白为什么,在基本的例子中,当只包含索引时。js文件直接在html中工作得很好,但是当webpack和包含在相同的html中处理时,它就不能工作了?我是不是漏掉了什么?

Everything works if not compiled with webpack ? WebPack let me write ES6 and split all things into one bundle but if i manually rewrite all files and use only one file it will work... So from my point of view, here it is more a webpack behave/config problem.

如果没有与webpack一起编译,所有的东西都能正常工作吗?WebPack让我可以编写ES6并将所有的东西分割成一个包,但是如果我手工重写所有的文件并且只使用一个文件,它将会工作…所以在我看来,这更像是webpack的行为/配置问题。

1 个解决方案

#1


7  

I finally found the solution, the problem was that the node_modules folder like sqlite was bundled in the bundle.js.

我最终找到了解决方案,问题是像sqlite这样的node_modules文件夹被绑定到bundl .js中。

The link below provide great explanation about that and explain the configuration for the backend with webpack and prevent the node_modules of being bundled when using the require directive.

下面的链接对此提供了很好的解释,并解释了使用webpack的后端配置,并防止在使用require指令时捆绑node_modules。

http://jlongster.com/Backend-Apps-with-Webpack--Part-I

http://jlongster.com/Backend-Apps-with-Webpack--Part-I

The code that solve my problem is :

解决我问题的代码是:

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

I then added nodeModules to externals config of the webpack

然后,我在webpack的外部配置中添加了nodeModules

module.exports = {

module: {
  .....
  externals: [nodeModules]
};

It also works for me if i set externals : ["sequelize"]

如果我设置了外部效果:["sequelize"]

#1


7  

I finally found the solution, the problem was that the node_modules folder like sqlite was bundled in the bundle.js.

我最终找到了解决方案,问题是像sqlite这样的node_modules文件夹被绑定到bundl .js中。

The link below provide great explanation about that and explain the configuration for the backend with webpack and prevent the node_modules of being bundled when using the require directive.

下面的链接对此提供了很好的解释,并解释了使用webpack的后端配置,并防止在使用require指令时捆绑node_modules。

http://jlongster.com/Backend-Apps-with-Webpack--Part-I

http://jlongster.com/Backend-Apps-with-Webpack--Part-I

The code that solve my problem is :

解决我问题的代码是:

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

I then added nodeModules to externals config of the webpack

然后,我在webpack的外部配置中添加了nodeModules

module.exports = {

module: {
  .....
  externals: [nodeModules]
};

It also works for me if i set externals : ["sequelize"]

如果我设置了外部效果:["sequelize"]