被 node 环境整疯了

2020-01-07 16:55:32 +08:00
 VensonEEE

一个普通 elctron 应用,集成进一个 sqlite3.因为结果回调很麻烦,换成了 better-sqlite3;
编译,开发运行一起正常,这里是指在npm run dev的环境下。
打包正常,运行的时候麻烦来了,一直报构造函数不是方法:

const sqlite = require('better-sqlite3');
this.db = new sqlite(file);

打包编译命令npm run electron:build
打包配置vue.config.js

module.exports = {
  configureWebpack: {
    module: {
      rules: [{
        test: /\.less$/,
        use: [{
          loader: 'less-loader', options: {
            javascriptEnabled: true,
          },
        }],
      }],
    },
    // Configuration applied to all builds
  },
  pluginOptions: {
    electronBuilder: {
      builderOptions: {
        'productName': xxx',
        'appId': 'xxx.xxx',
        'copyright': 'xxx',//版权  信息
        'dmg': {
          'contents': [
            {
              'x': 410,
              'y': 150,
              'type': 'link',
              'path': '/Applications',
            },
            {
              'x': 130,
              'y': 150,
              'type': 'file',
            },
          ],
        },
        'mac': {
          'icon': 'build/icons/icon.icns',
        },
        'win': {
          'icon': 'build/icons/icon.ico',
          'target': [
            {
              'target': 'nsis',
              'arch': [
                'ia32',
              ],
            },
          ],
        },
        'linux': {
          'icon': 'build/icons',
        },
        // 'asar': false,
        'directories': {
          'output': 'dist_electron',
          'buildResources': 'build',
          'app': 'dist_electron/bundled',
        },
        'files': ['**/*'],
        'extraResources': [
          { 
            'from': './public/assets',
            'to': './app/assets',
          },
        ],
        'nsis': {
          'oneClick': false,
          'allowToChangeInstallationDirectory': true,
          'createDesktopShortcut': true,
          'createStartMenuShortcut': true,
        },
      },
    },
  },
};

我怀疑是编译后的.node 文件没有被打包进去。 求吴彦祖大神审阅下工作。不甚感激。

10007 次点击
所在节点    Node.js
30 条回复
Rrrrrr
2020-01-07 17:00:37 +08:00
好歹把报错信息贴出来
sunzongzheng
2020-01-07 17:00:56 +08:00
VensonEEE
2020-01-07 17:05:45 +08:00
VensonEEE
2020-01-07 17:06:24 +08:00
@sunzongzheng 重新编译过了,不然 dev 就跑不起来
VensonEEE
2020-01-07 17:07:12 +08:00
@Rrrrrr
@sunzongzheng
传了图,求指导
BarZu
2020-01-07 17:12:27 +08:00
new sqlite.Sqlite() 这样子 ? 你应该输出 sqlite 看看是什么来的
VensonEEE
2020-01-07 17:14:42 +08:00
@BarZu 是发布版有问题。 dev 是能正常运行的。发布版的 console 自动都去了
项目地址:
https://www.npmjs.com/package/better-sqlite3
lovedebug
2020-01-07 17:16:24 +08:00
一般都是引用库的版本有问题
VensonEEE
2020-01-07 17:22:20 +08:00
![微信截图_20200107172108.png]( https://i.loli.net/2020/01/07/T7GxBQE1KFiCuez.png)

sqlite 好像是一个空对象,截图
jtwor
2020-01-07 17:29:18 +08:00
不是应该去看看 package.json
VensonEEE
2020-01-07 17:34:39 +08:00
@jtwor 运行版本 开发版本在 package.json 中没有单独配置项,所以开始就排除了。
jtwor
2020-01-07 17:39:39 +08:00
能运行是本地你已经下载了这个库在那个 node_model 什么的 但发布是读 pkg 配置的把 里面没有打包也没把
VensonEEE
2020-01-07 17:46:14 +08:00
@jtwor 确实是这样,不过我查了 pkg 也有。
```
{
"name": "xxx",
"version": "1.0.0",
"private": true,
"description": "xxx",
"author": "xxx",
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"dev": "vue-cli-service electron:serve",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
},
"main": "background.js",
"dependencies": {
"@types/strophe": "^1.2.31",
"@xmpp/client": "^0.9.1",
"@xmpp/debug": "^0.9.1",
"axios": "^0.18.0",
"better-sqlite3": "^5.4.3",
"bignumber.js": "^9.0.0",
"cropperjs": "^1.5.6",
"iview": "^3.5.4",
"md5-typescript": "^1.0.5",
"reflect-metadata": "^0.1.13",
"strophe": "^1.2.4",
"v-viewer": "^1.5.1",
"vue": "^2.6.11",
"vue-class-component": "^7.1.0",
"vue-i18n": "^8.15.3",
"vue-property-decorator": "^8.3.0",
"vue-router": "^3.1.3",
"vuex": "^3.1.2",
"vuex-class": "^0.3.2"
},
"devDependencies": {
"@types/jquery": "^3.3.31",
"@vue/cli-plugin-babel": "^3.12.1",
"@vue/cli-plugin-typescript": "^3.12.1",
"@vue/cli-service": "^3.12.1",
"electron": "^7.1.7",
"electron-rebuild": "^1.8.8",
"jquery": "^3.4.1",
"less": "^3.10.3",
"less-loader": "^5.0.0",
"node-sass": "^4.13.0",
"sass-loader": "^7.3.1",
"typescript": "^3.7.4",
"vue-cli-plugin-electron-builder": "^1.4.4",
"vue-template-compiler": "^2.6.11"
}
}

```
kimiler
2020-01-07 17:51:40 +08:00
为什么你这个帖子是黑色主题?
VensonEEE
2020-01-07 17:55:32 +08:00
@kimiler 我也不清楚... 看着有点费劲
ashong
2020-01-07 17:56:43 +08:00
@kimiler nodejs 就是黑的
UnluckyNinja
2020-01-07 18:05:06 +08:00
我看 better-sqlite3 官方文档没有写需要用 new 来调用吧
我猜测是因为开发环境和生产环境的语法严格程度不一样,导致一边报错另一边忽略了
不知楼主有没有用编辑器 linter 插件或者运行 lint 命令?

顺便吐槽下 nodejs 节点配色下的语法高亮,背景和字都是黑的,简直反人类
duan602728596
2020-01-07 18:14:05 +08:00
1. 可以试试把 require 替换成 global.require
2. 为什么不试试 IndexedDB 或者 WebSQL 呢
cppgohan
2020-01-07 18:17:17 +08:00
node 节点的现在的样式, 代码效果有点刺眼啊 @livid 老大看看
VensonEEE
2020-01-07 18:23:14 +08:00
@UnluckyNinja 感谢老哥的仔细,我刚刚把 new 加上和去了在 dev 都可以,发布版依然不行,还是熟悉的臭虫味道。

@duan602728596 需要在本地持久化存储一个文件。

我觉得是版本的问题。难道我 electron 和 node 的版本太高了?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/635856

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX