ElectronJs:请手动安装sqlite3包

时间:2018-03-30 23:34:29

标签: node.js sqlite electron

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

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

3 个答案:

答案 0 :(得分:14)

如果您在安装electron后收到错误无法找到节点模块sqlite3 请在sqlite应用程序中手动安装sqlite3包。然后,此解决方案可以帮助解决您的问题。

第1步:我们需要在您当地的

上克隆电子快速启动项目

来源: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

第4步:运行电子项目

$ npm start

现在让我们来看看最具挑战性的部分

第5步:现在我们需要安装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步:要解决此错误,我们需要安装电子重建包。在执行此命令之前,请确保使用cmd 以管理员身份运行,否则它将永远无法运行。

$ npm install --save-dev electron-rebuild

第9步:现在我们必须在脚本

下的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的地方)并点击确定按钮

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

$ npm run rebuild

第11步:现在你可以运行它了,它可以正常运行

$ npm start

快乐编程。

注意:如果您在运行npm run rebuild

时在步骤10中出现此错误
× 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

启动项目了

答案 1 :(得分: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

答案 2 :(得分:0)

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