我正在尝试在电子应用中使用node-sqlite3,但在应用窗口中,我在Chrome控制台中收到以下错误:
Uncaught TypeError: Cannot read property '_handle' of undefined
at file:///[...]/assets/js/bundle.js:38727:15
at Array.forEach (native)
at module.exports (file:///[...]/assets/js/bundle.js:38726:36)
at Object.<anonymous> (file:///[...]/assets/js/bundle.js:34699:1)
at Object.147._process (file:///[...]/assets/js/bundle.js:34999:4)
at s (file:///[...]/assets/js/bundle.js:1:254)
at file:///[...]/assets/js/bundle.js:1:305
at Object.<anonymous> (file:///[...]/assets/js/bundle.js:32065:11)
at Object.141.../package.json (file:///[...]/assets/js/bundle.js:32246:4)
at s (file:///[...]/assets/js/bundle.js:1:254)
错误发生在file:///[...]/assets/js/node_modules/sqlite3/node_modules/set-blocking/index.js
的第3行(此文件自动生成并删除,我没有写入):
module.exports = function (blocking) {
[process.stdout, process.stderr].forEach(function (stream) {
if (stream._handle && stream.isTTY && typeof stream._handle.setBlocking === 'function') {
stream._handle.setBlocking(blocking)
}
})
}
只有在渲染器进程中使用const sqlite3 = require('sqlite3')
时(例如在React组件中)才会发生这种情况。 require('sqlite3')
在主电子过程中使用时没有任何问题(数据库调用工作)。
This example app表明可以在渲染器进程中使用sqlite模块。我不明白为什么它在我的情况下不起作用。
我的package.json
:
{
...,
"scripts": {
"postinstall": "install-app-deps",
"start": "electron .",
"watch": "watchify app/app.js -t babelify -o assets/js/bundle.js --debug --verbose",
"watch-style": "sass -r sass-globbing --watch style/application.scss:assets/css/bundle.css"
},
"devDependencies": {
"babel-preset-stage-2": "^6.24.1",
"electron-builder": "^19.49.4"
},
"dependencies": {
"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babelify": "^8.0.0",
"browserify": "^14.5.0",
"electron": "^1.7.10",
"electron-reload": "^1.2.2",
"electron-window-state": "^4.1.1",
"react": "^16.2.0",
"sqlite3": "^3.1.13",
"watchify": "^3.9.0",
...
}
}
我使用:node 7.9.0,chrome 58.0.3029.110,electron 1.7.10。
我在github here上创建了一个问题。
答案 0 :(得分:3)
它在渲染器进程中不起作用的原因是它是通过browserify捆绑的,它旨在为const moment = require('moment');
const generateSearchRange = (year) => (month) => {
return {
start: 0,
stop: moment([year]).endOf('year')
}
};
console.log(generateSearchRange(2017)(12)); // { start: 0, stop: moment("2017-12-31T23:59:59.999") }
生成包。您失败的特定代码依赖于node.js侧全局(进程),因此browserify无法正确捆绑它。而且,sqlite3模块里面有本机模块,不能捆绑。像webpack这样的其他捆绑包有方法(browser
选项)指定不尝试捆绑它,如果它支持,你可能需要使用browserify以类似的方式配置。
它还解释了为什么示例应用程序有效,它不会为渲染器进程进行任何捆绑。