【问题标题】:ElectronJs: please install sqlite3 package manuallyElectronJs:请手动安装 sqlite3 包
【发布时间】:2018-09-09 23:38:41
【问题描述】:

我试图用electron 应用程序配置sqlite 数据库,在安装sqlite 依赖项后,我开始收到错误请手动安装sqlite3 包。我多次重复相同的步骤,但sqlite 不适用于electron

我只是在互联网上搜索这个问题,我找到了几个答案,但每个答案都缺少步骤。因此,在这个问题上花了我 4 个工作日后,我想出了一个包含所有详细步骤的可靠解决方案。希望它可以为其他人节省时间

【问题讨论】:

    标签: node.js sqlite electron


    【解决方案1】:

    如果您在安装sqlite 后在electron 应用程序中收到错误找不到节点模块sqlite3请手动安装sqlite3 包。那么这个解决方案可以帮助您解决问题。

    第 1 步:我们需要在您的本地克隆 electron 快速启动项目

    来源:https://electronjs.org/docs/tutorial/first-app

    $ git clone https://github.com/electron/electron-quick-start
    

    第 2 步:进入存储库

    $ cd electron-quick-start
    

    第 3 步: 安装 electronJs 依赖项

    $ npm install
    

    第四步:运行电子项目

    $ npm start
    

    现在让我们进入最具挑战性的部分

    第五步:现在我们需要安装sqlite依赖

    $ npm install sqlite3 --save
    $ npm install sequelize --save
    

    第 6 步: 在 IDE 中打开 main.js 并将此代码添加到文件末尾以创建数据库连接

    来源:http://docs.sequelizejs.com/manual/installation/usage.html

    // Create database connection
    const Sequelize = require('sequelize');
        const sequelize = new Sequelize('database', 'username', 'password', {
        host: 'localhost',
        dialect: 'sqlite',
        operatorsAliases: false,
        pool: {
            max: 5,
            min: 0,
            acquire: 30000,
            idle: 10000
        },
        storage: './database.sqlite'
    });
    
    // Test connection
    sequelize
        .authenticate()
        .then(() => {
            console.log('Connection has been established successfully.');
        })
        .catch(err => {
            console.error('Unable to connect to the database:', err);
        });
    

    第 7 步:现在再次使用 npm start 启动您的电子项目,您将开始收到异常

    Error: Please install sqlite3 package manually
    

    我们如何解决它?

    第 8 步: 要修复此错误,我们需要安装 electron-rebuild 包。在执行此命令之前,请确保您使用 cmd 以管理员身份运行,否则它将永远无法工作。

    $ npm install --save-dev electron-rebuild
    

    第九步:现在我们要在脚本下package.json配置rebuild命令

    "scripts": {
        "rebuild": "electron-rebuild -f -w sqlite3"
    }
    

    第 10 步: 现在我们需要安装 python 并将其设置为环境变量中的 Path。

    • 下载Python:https://www.python.org/download/releases/2.7/
    • 打开控制面板>系统和安全>系统>高级系统设置>环境变量
    • %username% 的用户变量 部分下,单击新建以添加新项目
    • 设置变量名Path和变量值C:\Python27(安装python的地方),点击OK按钮

    第 10 步:现在打开一个新命令行并重建您的电子项目

    $ npm run rebuild
    

    第 11 步:现在您可以运行它,它会正常工作

    $ npm start
    

    快乐编程。


    注意:如果您在第 10 步运行npm run rebuild时收到此错误@

    × Rebuild Failed
    
    An unhandled error occurred inside electron-rebuild
    Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
    MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere.  [C:\Projects\Test\node_modules\sqlite3\build\binding.sln]
    gyp ERR! build error
    gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
    gyp ERR! stack     at ChildProcess.onExit (C:\Projects\Test\node_modules\node-gyp\lib\build.js:262:23)
    gyp ERR! stack     at emitTwo (events.js:106:13)
    gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
    gyp ERR! System Windows_NT 10.0.17134
    gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Projects\\Test\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--target=1.8.8" "--arch=x64" "--dist-url=https://atom.io/download/electron" "--build-from-source" "--module_name=node_sqlite3" "--module_path=C:\\Projects\\Test\\node_modules\\sqlite3\\lib\\binding\\electron-v1.8-win32-x64" "--host=https://mapbox-node-binary.s3.amazonaws.com" "--remote_path=./{name}/v4.0.2/{toolset}/" "--package_name=electron-v1.8-win32-x64.tar.gz"
    gyp ERR! cwd C:\\Projects\\Test\node_modules\sqlite3
    gyp ERR! node -v v6.9.5
    gyp ERR! node-gyp -v v3.8.0
    gyp ERR! not ok
    
    Failed with exit code: 1
    
    Error: Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
    MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere.  [C:\Projects\Test\node_modules\sqlite3\build\binding.sln]
    gyp ERR! build error
    gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
    gyp ERR! stack     at ChildProcess.onExit (C:\Projects\Test\node_modules\node-gyp\lib\build.js:262:23)
    gyp ERR! stack     at emitTwo (events.js:106:13)
    gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
    gyp ERR! System Windows_NT 10.0.17134
    gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Projects\\Test\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--target=1.8.8" "--arch=x64" "--dist-url=https://atom.io/download/electron" "--build-from-source" "--module_name=node_sqlite3" "--module_path=C:\\Projects\\Test\\node_modules\\sqlite3\\lib\\binding\\electron-v1.8-win32-x64" "--host=https://mapbox-node-binary.s3.amazonaws.com" "--remote_path=./{name}/v4.0.2/{toolset}/" "--package_name=electron-v1.8-win32-x64.tar.gz"
    gyp ERR! cwd C:\\Projects\\Test\node_modules\sqlite3
    gyp ERR! node -v v6.9.5
    gyp ERR! node-gyp -v v3.8.0
    gyp ERR! not ok
    
    Failed with exit code: 1
        at SafeSubscriber._error (C:\Projects\Test\node_modules\spawn-rx\lib\src\index.js:277:84)
        at SafeSubscriber.__tryOrUnsub (C:\Projects\Test\node_modules\rxjs\Subscriber.js:242:16)
        at SafeSubscriber.error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:201:26)
        at Subscriber._error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:132:26)
        at Subscriber.error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:106:18)
        at MapSubscriber.Subscriber._error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:132:26)
        at MapSubscriber.Subscriber.error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:106:18)
        at SafeSubscriber._next (C:\Projects\Test\node_modules\spawn-rx\lib\src\index.js:251:65)
        at SafeSubscriber.__tryOrSetError (C:\Projects\Test\node_modules\rxjs\Subscriber.js:251:16)
        at SafeSubscriber.next (C:\Projects\Test\node_modules\rxjs\Subscriber.js:191:27)
    

    然后在命令行中执行这个npm install windows-build-tools --global

    在安装 windows-build-tools 时是否会出现此异常

    async function aquireInstallers(cb) {
          ^^^^^^^^
    SyntaxError: Unexpected token function
        at Object.exports.runInThisContext (vm.js:76:16)
        at Module._compile (module.js:542:28)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.require (module.js:497:17)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\windows-build-tools\dist\start.js:4:29)
    

    这意味着你正在使用旧版本的 nodeJS 来修复它,你可以使用 npm install windows-build-tools@2.2.1 --global

    安装旧版本的 windows-build-tools

    现在您可以使用npm start启动项目

    【讨论】:

    • 在某些情况下,也需要做npm install -g windows-build-toolselectron-rebuild failure with sqlite3 · Issue #204 · electron/electron-rebuild
    • 结构良好的答案。
    • 很好的答案,但就我而言,我需要做npm install -g windows-build-tools --vs2015(因为当前版本是2017,但node-gyp还不支持)。使用中速连接执行安装需要很长时间(可能半小时)。
    • 另外,在我的系统(运行 Windows 10)上,我需要将“C:\Users[username]\.windows-build-tools\python27\”添加到 Environment Variables > User variables > 路径,而不是“C:\python27”。
    【解决方案2】:

    如果你通过 sequelize 使用 sqlite3,你可以通过像这样将 sequelize 添加到 webpack.config 的外部来避免错误。

    module.exports = {
      // other configs ..
      externals: {
        "sequelize": "require('sequelize')",
      },
    }
    

    参考:https://www.bountysource.com/issues/38723672-can-not-pack-sequelize-with-sqlite3

    【讨论】:

      【解决方案3】:

      我有同样的问题,为了解决它,我只是更改了节点版本。 我在版本 14.0.0 上运行并收到错误。 然后将节点版本更改为 12.0.0,一切正常!

      【讨论】:

        【解决方案4】:

        我通过在sequelize 中使用dialectModule 选项解决了这个问题。

        // db.ts
        
        import sqlite3 from "sqlite3"
        import { Sequelize } from "sequelize"
        
        const sequelize = new Sequelize( {
          dialect: "sqlite",
          dialectModule: sqlite3, // Set it here.
          storage: ":memory:",
        } )
        

        【讨论】:

          猜你喜欢
          • 2019-11-17
          • 2018-10-20
          • 2014-11-15
          • 1970-01-01
          • 2019-11-18
          • 1970-01-01
          • 2020-01-29
          • 2018-11-09
          • 2011-11-15
          相关资源
          最近更新 更多