vite-electron-quick icon indicating copy to clipboard operation
vite-electron-quick copied to clipboard

安装的 serialport 模块在打包之后生成的 exe 文件报错,找不到模块

Open Dlouxgit opened this issue 3 years ago • 7 comments

npm run dev 可以正常运行,并调用这个模块的功能, npm run build 后生成的 .exe 文件会出现如下情况 image

Dlouxgit avatar Mar 03 '21 07:03 Dlouxgit

我是 MacOS 没有出现这个问题,建议按顺序重试:

  • git pull 最新代码,或重新使用 npx create-vite-electron 下载模版
  • 把 node_modules/ 删掉
  • package-lock.json 或 yarn.lock 删掉
  • 重新使用 yarn 安装

如果还有问题的话,等我有windows系统我再看一下~

MangoTsing avatar Mar 03 '21 07:03 MangoTsing

我尝试了 git pull 和 npx create-vite-electron 重新下载模板, 仍然会报错,一般模块都是没有问题的,只有这个模块(功能是调用串口),我只在 main.ts 加上一行 const Serialport = require('serialport') 后打包,打开 win-unpacked 下的 .exe 文件就会出现这样的报错。

如果我改用 import Serialport from 'serialport' 报错会变成 image

Dlouxgit avatar Mar 03 '21 10:03 Dlouxgit

ffi-napi模块同样的情况 image

plus1998 avatar Mar 03 '21 11:03 plus1998

我把node_modules/ffi-napi添加到build.files中起效果了,但是遇到cannot find module lodash.defaults的时候这个方法就无效了。大佬有点什么思路码

plus1998 avatar Mar 05 '21 01:03 plus1998

我把node_modules/ffi-napi添加到build.files中起效果了,但是遇到cannot find module lodash.defaults的时候这个方法就无效了。大佬有点什么思路码

猜测哈,应该是你ffi-napi里面需要用到lodash的模块,你试试把node_modules目录都打进build.files去...

MangoTsing avatar Mar 05 '21 06:03 MangoTsing

我尝试了 git pull 和 npx create-vite-electron 重新下载模板, 仍然会报错,一般模块都是没有问题的,只有这个模块(功能是调用串口),我只在 main.ts 加上一行 const Serialport = require('serialport') 后打包,打开 win-unpacked 下的 .exe 文件就会出现这样的报错。

如果我改用 import Serialport from 'serialport' 报错会变成 image

你这个报错是 require 在 vite 下打包并没有被解析,两个办法可以解决;

  1. 考虑开个 vite 插件把 serialport 打包进来
  2. 考虑打包期用 webpack 构建

caoxiemeihao avatar May 22 '21 02:05 caoxiemeihao

试试这个模板 💪 Full Featured. Enabled NodeJs in Renderer-process. SQLite3/serialport/electron-store. https://github.com/caoxiemeihao/electron-vite-boilerplate

caoxiemeihao avatar Jan 25 '22 12:01 caoxiemeihao