ElectronJs:请手动安装sqlite3包

UFF*_*FAN 8 sqlite node.js electron

我试图sqliteelectron应用程序配置数据库,安装sqlite依赖后我开始收到错误请手动安装sqlite3包.我重复了几次相同的步骤但是sqlite没有用electron

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

UFF*_*FAN 18

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

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

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

$ git clone https://github.com/electron/electron-quick-start
Run Code Online (Sandbox Code Playgroud)

第2步:进入存储库

$ cd electron-quick-start
Run Code Online (Sandbox Code Playgroud)

第3步:安装electronJs依赖项

$ npm install
Run Code Online (Sandbox Code Playgroud)

第4步:运行电子项目

$ npm start
Run Code Online (Sandbox Code Playgroud)

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

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

$ npm install sqlite3 --save
$ npm install sequelize --save
Run Code Online (Sandbox Code Playgroud)

步骤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);
    });
Run Code Online (Sandbox Code Playgroud)

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

Error: Please install sqlite3 package manually
Run Code Online (Sandbox Code Playgroud)

我们如何解决它?

第8步:要修复此错误,我们需要安装电子重建包.在执行此命令之前,请确保使用cmd 以管理员身份运行,否则它将无法运行.

$ npm install --save-dev electron-rebuild
Run Code Online (Sandbox Code Playgroud)

步骤9:现在我们必须package.json在脚本下配置rebuild命令

"scripts": {
    "rebuild": "electron-rebuild -f -w sqlite3"
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

$ npm run rebuild
Run Code Online (Sandbox Code Playgroud)

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

$ npm start
Run Code Online (Sandbox Code Playgroud)

快乐的编程.


注意:如果在运行时在步骤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)
Run Code Online (Sandbox Code Playgroud)

然后在命令行中执行此操作 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)
Run Code Online (Sandbox Code Playgroud)

这意味着你使用的是旧版本的的NodeJS修复它,你可以安装旧版本的Windows的构建工具使用npm install windows-build-tools@2.2.1 --global

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

  • 结构良好的答案。 (4认同)