我用 express.js 创建了一个 Electron 应用程序,用 Electron 开发一个快速应用程序的原因是打包一个在没有安装 Node 的情况下运行的可执行文件。
我成功地将应用程序打包到 centos 7 中。如果我双击来自 centos GUI 的应用程序二进制文件,它运行良好。当我尝试通过 putty 使用命令行终端登录运行它时,使用 ./app
什么也没发生,我不明白为什么。
有什么方法可以运行从 putty 的 Electron 打包程序登录生成的 centos 二进制文件?
源码如下,我用来打包的命令是npm run build
注意:当我尝试使用 ./app
使用 GUI 终端运行它时它再次正常工作
main.js
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
var express = require('express');
var _app = express();
const server = require('http').Server(_app);
let PORT = 3001;
_app.use(express.static(__dirname + '/public'));
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
_app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
let win
function createWindow () {
win = new BrowserWindow({width: 800, height: 600})
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
win.webContents.openDevTools()
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
包.json
{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron .",
"build": "electron-packager . app --platform linux --arch x64 --out dist/",
"rpm64": "electron-installer-redhat --src dist/app-linux-x64/ --dest dist/installers/ --arch x86_64",
"pack": "build --dir",
"dist": "build"
},
"build": {
"appId": "myapp",
"category": "your.app.category.type",
"dmg": {
"contents": [
{
"x": 110,
"y": 150
},
{
"x": 240,
"y": 150,
"type": "link",
"path": "/root/projects/myapp"
}
]
},
"linux": {
"target": [
"AppImage",
"deb"
]
},
"win": {
"target": "squirrel",
"icon": "build/icon.ico"
}
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^2.0.0",
"electron-builder": "^20.11.1",
"electron-installer-redhat": "^0.5.0",
"electron-packager": "*",
"electron-prebuilt": "*"
},
"dependencies": {
"electron-terminal-open": "^0.1.1",
"express": "^4.16.3"
}
最佳答案
因为这是一个类似于应用程序的 Web 服务器,所以我能够将它与 Node 包一起部署到 node.js os 没有安装和运行它的地方,没有任何问题。以下是步骤
在我的 centos 7 上的 Node 应用程序中,我使用 npm i
安装了所有依赖项
然后我将 node.js linux 二进制文件下载到 app 文件夹并 tar.gz 。
然后我将它移动到另一个没有安装 Node 的centos pc。
我解压缩 tar.gz 文件并将 PATH 设置为 node.js 文件夹,在我的应用程序文件夹中以下列方式 PATH=$PATH:/path/to/my/app/node/bin
然后我运行 npm start
它工作正常,没有任何问题。我在这里没有使用 Electron ,也找不到 Electron 问题的答案。
关于node.js - 如何在命令行终端(vty)中运行 Electron 二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50231185/