将 console.log() 与电子一起使用

2022-01-10 00:00:00 node.js electron javascript console.log

我已经看到很多人试图从渲染过程中控制日志,这不是我的问题我有 console.log 乱扔我的主要代码,我在控制台中看不到任何东西这是我的代码.

I have seen a lot of questions from people trying to console log from the rendering process, this is NOT my problem I have console.log littering my main code and I don't see anything in my console here is my code.

/* eslint-disable no-undef */
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
/* eslint-enable */

let win;

console.log('console log test');

function createWindow() {
  win = new BrowserWindow({
    width: 800,
    height: 800
  });

  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }));

  win.on('close', () => {
    win = null;
  });

  console.log('console log test');
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (win == null) {
    console.log('console log test');
    createWindow();
    console.log('console log test');
  }
});

除了电子本身产生的日志之外,我没有看到任何日志我尝试抛出错误并且这些工作正常,但是任何与控制台相关的东西都不起作用,我尝试在 PowerShell 中运行它并从 GitHub 重新拉取,我的朋友在拉取项目时可以看到控制台日志虽然如此看来我是孤立的.我还更新了 NPM 和与项目相关的所有模块,并且我尝试创建一个新控制台并登录到该控制台,但它似乎没有出现,我错过了什么吗?我已经为此投入了数小时,并准备放弃.

I don't see a single log other than the ones produced by electron itself I've tried throwing errors and those work fine but anything console.* related doesn't work at all, I've tried running it in PowerShell and re-pulling from GitHub, my friend can see the console logs when he pulls the project though so it seems I'm isolated. I've also updated NPM and all modules associated with the project AND I've tried creating a new console and logging to that one but it doesn't seem to show up, am I missing something? I've put hours into this and am ready to give up.

推荐答案

我感觉到你的痛苦.我的一个盒子(一个 Server2012 盒子)有这个问题.直到我偶然发现 此评论 上的一个电子对我有用问题线程.

I feel your pain. I have this issue on one of my boxes (a Server2012 box). Nothing worked for me until I stumbled across this comment on one of the electron issues threads.

通常,当您安装 electron 时,您的 package.json 中会有一个如下所示的脚本.

Typically when you install electron you will have a script in your package.json that looks like this.

"scripts": {
    "start": "electron .",
}

我改成了

"scripts": {
    "start": "C:/path/to/project/node_modules/electron-prebuilt/dist/electron.exe .",
}

我开始从 powershell 中的主电子进程获取日志记录.

And I started to get logging from the main electron process in powershell.

请注意,如果您使用较新版本的电子,您可能需要将 electron-prebuilt 更改为 electron.

Note that if you are using newer versions of electron, you may need to change electron-prebuilt to electron.

相关文章