electron - 当 package.json 包含安装后脚本时,为什么 Node 串行端口会失败?
问题描述
添加此语句
const port = new serialport('COM1',
{ autoOpen: true,
baudRate: 9600,
stopBits: 1},
((err) => {if (err){console.log('Error:', err)}
else {console.log('COM1 opened!')}})
)
到 Electron-serialport-quick-start ( https://github.com/serialport/electron-serialport-quick-start ) 导致 'npm run start' 失败(在 'rm -rf node_modules'、'rm package-lock. json', 'npm i' 命令序列)如下,如果推荐的 'postinstall' 脚本包含在 package.json 中:
Error: TypeError: Third argument must be a function
at internal/util.js:297:30
at new Promise (<anonymous>)
at open (internal/util.js:296:12)
at WindowsBinding.open (G:\CodeDev\quickStartSerialPort\node_modules\@serialport\bindings\lib\win32.js:56:22)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
有或没有可选的构造函数参数都会出现故障,同样,如果 autoOpen 设置为 false 并且随后调用 port.open (据我所知,port.open 的回调在串行端口文档中未被标识为选项)。
不知道这是串口还是安装后脚本问题,但如果从以下 package.json 中省略安装后脚本,可以确认端口打开时没有错误:
{
"name": "electron-serialport",
"version": "1.0.2",
"description": "A minimal Electron application with node serialport",
"main": "main.js",
"scripts": {
"start": "electron .",
"install": "electron-rebuild",
"postinstall": "electron-builder install-app-deps",
"build": "electron-builder -w nsis"
},
"repository": {
"type": "git",
"url": "git@github.com:serialport/electron-serialport.git"
},
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^11.2.0",
"electron-rebuild": "^2.3.4"
},
"dependencies": {
"serialport": "^9.0.7",
"tableify": "^0.0.3"
}
}
该问题出现在 Windows VS Code git bash 环境中;它尚未在其他平台上进行过测试。
解决方案
推荐阅读
- c - 我可以在一个脚本的多个线程中安全地运行 FCGI_Accept() 吗?
- javascript - Android WebView 焦点
- gcc - variable attributes changes between GCC versions - "nobits"
- html - Mathjax 方程在 Android 手机中的渲染很小
- javascript - 如何使用 Javascript 禁用 webrtc 麦克风?
- tensorflow - 如何使用 Tensorflow Serving 在 AI 平台上创建 HTTP 服务器
- javascript - fnPageChange 不是函数
- c#-4.0 - 水平打开 XML 2.0 Word 文档合并单元格
- python - 使用 Python 打印与平均值偏差 1 范围内的所有数字
- mysql - Sequlize Fetch 没有关联子级的行