【electron】打包报错webpack/electron/sqlite3/node-pre-pyg代码被打包进主线程js文件

(base) songjunwei@songjunweideMacBook-Pro viefong-pc % yarn build:main
yarn run v1.22.21
$ cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./electron/.erb/configs/webpack.config.main.prod.ts
ERROR in ./node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>
 @ ./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/nw-pre-gyp/index.html
 @ ./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 86:13-36 291:36-53
 @ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:15-46
 @ ./node_modules/sqlite3/lib/sqlite3.js 2:16-47
 @ ./electron/main/db/createDB.ts 2:16-42
 @ ./electron/main/main.ts

ERROR in ./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 43:20-42
Module not found: Error: Can't resolve 'mock-aws-s3' in '/Users/songjunwei/Documents/newmine/viefong-pc/node_modules/@mapbox/node-pre-gyp/lib/util'
 @ ./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:15-46
 @ ./node_modules/sqlite3/lib/sqlite3.js 2:16-47
 @ ./electron/main/db/createDB.ts 2:16-42
 @ ./electron/main/main.ts

ERROR in ./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 76:14-32
Module not found: Error: Can't resolve 'aws-sdk' in '/Users/songjunwei/Documents/newmine/viefong-pc/node_modules/@mapbox/node-pre-gyp/lib/util'
 @ ./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:15-46
 @ ./node_modules/sqlite3/lib/sqlite3.js 2:16-47
 @ ./electron/main/db/createDB.ts 2:16-42
 @ ./electron/main/main.ts

ERROR in ./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 112:15-30
Module not found: Error: Can't resolve 'nock' in '/Users/songjunwei/Documents/newmine/viefong-pc/node_modules/@mapbox/node-pre-gyp/lib/util'
 @ ./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:15-46
 @ ./node_modules/sqlite3/lib/sqlite3.js 2:16-47
 @ ./electron/main/db/createDB.ts 2:16-42
 @ ./electron/main/main.ts

ERROR in ./node_modules/node-gyp/lib/Find-VisualStudio.cs 9:6
Module parse failed: Unexpected token (9:6)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| // This script needs to be compatible with PowerShell v2 to run on Windows 2008R2 and Windows 7.
| 
> using System;
| using System.Text;
| using System.Runtime.InteropServices;
 @ ./node_modules/node-gyp/lib/ sync ^\.\/.*$ ./Find-VisualStudio.cs
 @ ./node_modules/node-gyp/lib/node-gyp.js 41:13-36 195:36-53
 @ ./node_modules/@mapbox/node-pre-gyp/lib/util/compile.js 31:26-53
 @ ./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/compile.js ./util/compile
 @ ./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 86:13-36 291:36-53
 @ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:15-46
 @ ./node_modules/sqlite3/lib/sqlite3.js 2:16-47
 @ ./electron/main/db/createDB.ts 2:16-42

解决:

安装:
yarn add -D webpack-node-externals
引入webpack中:
const nodeExternals = require('webpack-node-externals');
重点两行:
	externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
	externals: [ 'nock', 'mock-aws-s3', 'aws-sdk', nodeExternals()],

**加粗样式
**

以上参考:
https://juejin.cn/post/6844904104980905998
https://www.npmjs.com/package/webpack-node-externals
分界线------------------------------------------------
这样是可以解决我昨天打包时报错的问题,但打包后再打开安装包会一直报各种我在electron中所引入的依赖文件,一直解决一直报错,我有感觉就是这个插件所引起的问题:所以我就去看webpack-node-externals这个插件的究竟要解决的问题和我目前所引起的问题究竟是什么?
在这里插入图片描述
我已经解决了这个问题,所以我用了别人的一张图
在这里插入图片描述
我后来将webpack-node-externals这个插件去掉,然后安装
node-pre-gyp github

npm install @mapbox/node-pre-gyp --save

// 注:我这里的系统已经配置了python等等环境,项目也安装了electron-rebuild包,所以我这里只需要执行下:
./node_modules/.bin/electron-rebuild

参考:https://newsn.net/say/electron-sqlite3.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值