caoxiemeihao / nuxt-electron

Integrate Nuxt and Electron
MIT License
168 stars 18 forks source link

Strange loop #74

Open angelhdzmultimedia opened 2 months ago

angelhdzmultimedia commented 2 months ago

I added nuxt-electron to my fully functional nuxt app, the window opens, Nuxt loads, then it starts a strange loop and browser nuxt restarts constantly and tons of errors in the terminal and browser console... so I created a fresh Nuxt app and added nuxt-electron and the same. Downgraded electron from v30 to v28, same. 😭

jbhaywood commented 2 months ago

I just encountered the same thing.

ERROR [unhandledRejection] write after end 10:06:22 PM

at __node_internal_captureLargerStackTrace (node:internal/errors:497:5) at new NodeError (node:internal/errors:406:5) at _write (node:internal/streams/writable:378:11) at Writable.write (node:internal/streams/writable:393:10) at Sender.sendFrame (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58623:20) at Sender.close (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58351:12) at WebSocket.close (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:59431:18) at Receiver.receiverOnConclude (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:60277:18) at Receiver.emit (node:events:514:28) at Receiver.controlMessage (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58081:14) at Receiver.getData (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57897:12) at Receiver.startLoop (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57587:16) at Receiver._write (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57514:10) at writeOrBuffer (node:internal/streams/writable:447:12) at _write (node:internal/streams/writable:389:10) at Writable.write (node:internal/streams/writable:393:10) at Socket.socketOnData (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:60426:37) at Socket.emit (node:events:514:28) at addChunk (node:internal/streams/readable:376:12) at readableAddChunk (node:internal/streams/readable:349:9) at Readable.push (node:internal/streams/readable:286:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

ERROR [unhandledRejection] write after end 10:06:23 PM

at __node_internal_captureLargerStackTrace (node:internal/errors:497:5) at new NodeError (node:internal/errors:406:5) at _write (node:internal/streams/writable:378:11) at Writable.write (node:internal/streams/writable:393:10) at Sender.sendFrame (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58623:20) at Sender.close (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58351:12) at WebSocket.close (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:59431:18) at Receiver.receiverOnConclude (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:60277:18) at Receiver.emit (node:events:514:28) at Receiver.controlMessage (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58081:14) at Receiver.getData (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57897:12) at Receiver.startLoop (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57587:16) at Receiver._write (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57514:10) at writeOrBuffer (node:internal/streams/writable:447:12) at _write (node:internal/streams/writable:389:10) at Writable.write (node:internal/streams/writable:393:10) at Socket.socketOnData (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:60426:37) at Socket.emit (node:events:514:28) at addChunk (node:internal/streams/readable:376:12) at readableAddChunk (node:internal/streams/readable:349:9) at Readable.push (node:internal/streams/readable:286:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

ERROR [unhandledRejection] read ECONNRESET 10:06:23 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:23 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:24 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:24 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:24 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:25 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:25 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] write after end 10:06:26 PM

at __node_internal_captureLargerStackTrace (node:internal/errors:497:5) at new NodeError (node:internal/errors:406:5) at _write (node:internal/streams/writable:378:11) at Writable.write (node:internal/streams/writable:393:10) at Sender.sendFrame (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58623:20) at Sender.close (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58351:12) at WebSocket.close (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:59431:18) at Receiver.receiverOnConclude (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:60277:18) at Receiver.emit (node:events:514:28) at Receiver.controlMessage (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:58081:14) at Receiver.getData (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57897:12) at Receiver.startLoop (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57587:16) at Receiver._write (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:57514:10) at writeOrBuffer (node:internal/streams/writable:447:12) at _write (node:internal/streams/writable:389:10) at Writable.write (node:internal/streams/writable:393:10) at Socket.socketOnData (/D:/OneDrive/Active%20Projects/Quill%20Example%20Repositories/build_test/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:60426:37) at Socket.emit (node:events:514:28) at addChunk (node:internal/streams/readable:376:12) at readableAddChunk (node:internal/streams/readable:349:9) at Readable.push (node:internal/streams/readable:286:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

ERROR [unhandledRejection] read ECONNRESET 10:06:26 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:26 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:27 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:27 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

ERROR [unhandledRejection] read ECONNRESET 10:06:27 PM

at node_internal_captureLargerStackTrace (node:internal/errors:497:5) at node_internal_errnoException (node:internal/errors:624:12) at TCP.onStreamRead (node:internal/stream_base_commons:217:20)

caoxiemeihao commented 2 months ago

I ran into the same issue and am trying to find the cause of the BUG in nuxt's source code. At present, I found that the nuxt startup port is 3000 by default, but in the hooks of nuxt-electron, I always get a random port, which will cause Electron to fall into an infinite loop after startup.

image

I'm stuck on this, maybe someone can give me some help? Thanks in advance. @danielroe 🙏

caoxiemeihao commented 2 months ago

Eventually I found out that this was because nuxt’s Performance mode would fork the main process once, causing nuxt-electron’s hooks to listen to the wrong(random) server.port.

I think this is an issue that needs improvement.

Fork main process https://github.com/nuxt/cli/blob/v3.11.1/src/commands/dev.ts#L68-L73

image

hooks.listen only runs in the child process after the fork, causing server.port to become a random number. https://github.com/nuxt/cli/blob/v3.11.1/src/utils/dev.ts#L262-L266

image

Currently, we can temporarily avoid this issue in the following ways.

{
  "scripts": {
-   "dev": "nuxi dev",
+   "dev": "nuxi dev --no-fork",
    "build": "nuxi build --prerender && electron-builder"
  }
}
angelhdzmultimedia commented 2 months ago

Eventually I found out that this was because nuxt’s Performance mode would fork the main process once, causing nuxt-electron’s hooks to listen to the wrong(random) server.port.

I think this is an issue that needs improvement.

Fork main process https://github.com/nuxt/cli/blob/v3.11.1/src/commands/dev.ts#L68-L73 image

hooks.listen only runs in the child process after the fork, causing server.port to become a random number. https://github.com/nuxt/cli/blob/v3.11.1/src/utils/dev.ts#L262-L266 image

Currently, we can temporarily avoid this issue in the following ways.

{
  "scripts": {
-   "dev": "nuxi dev",
+   "dev": "nuxi dev --no-fork",
    "build": "nuxi build --prerender && electron-builder"
  }
}

Thank you so much. Will try in a few hours. Hope you have a nice rest of the day. Lots of health, peace, and success.

angelhdzmultimedia commented 2 months ago

Update

Modified the dev script as you mentioned. Error continues. But found something interesting... if I run bun --bun run dev (which runs the script with Bun runtime instead of Node), it works right away and fast!!!

Node:

image

[25552:0417/174211.789:ERROR:CONSOLE(1)] "Request Autofill.enable failed. {"code":-32601,"message":"'Autofill.enable' wasn't found"}", source: devtools://devtools/bundled/core/protocol_client/protocol_client.js (1)

Bun:

image

jbhaywood commented 2 months ago

For anyone else that comes here for this issue, @caoxiemeihao's fix worked for me. I'm not using Bun, fwiw.

angelhdzmultimedia commented 2 months ago

For anyone else that comes here for this issue, @caoxiemeihao's fix worked for me. I'm not using Bun, fwiw.

I keep having the issue, switched to pnpm. Deleted node_modules. Ran pnpm install and pnpm run dev which points to the dev script with the --no-fork boolean flag. 😔

Edit: Upgraded pnpm from v8 to v9. Executed pnpx nuxi upgrade -f to install latest nuxt deps. Then pnpm run dev which points to "dev": "nuxt dev --no-fork". Same error.

angelhdzmultimedia commented 2 months ago

Hmmm... the loop is gone... new error: image

Edit: Nevermind. Loop is back.

caoxiemeihao commented 2 months ago

@angelhdzmultimedia You can try to use the latest quick-start, I downgraded nuxt to 3.6.5 and everything works fine.

danielroe commented 2 months ago

The issue here is not Nuxt but a change in behavior in nuxi itself, which is now versioned and developed separately. You can likely downgrade your version of nuxi - but note there are some issues in that repo that explain what's happening and likely give some tips on how to work around it.

angelhdzmultimedia commented 2 months ago

@angelhdzmultimedia You can try to use the latest quick-start, I downgraded nuxt to 3.6.5 and everything works fine.

Oooh downgrading to nuxt 3.6.5 did it! Thanks! 🤯🙏 image

Patrity commented 2 months ago

Thanks for developing this great module @caoxiemeihao I was reliant on it for a couple of my apps, but currently can not use it without downgrading multiple packages which are needed. The --no-fork flag did not end up working for me, but I'm commenting to stay in the loop on hopefully any future updates/fixes.

angelhdzmultimedia commented 2 months ago

Thanks for developing this great module @caoxiemeihao I was reliant on it for a couple of my apps, but currently can not use it without downgrading multiple packages which are needed. The --no-fork flag did not end up working for me, but I'm commenting to stay in the loop on hopefully any future updates/fixes.

You also need to downgrade nuxt package to 3.6.5

Patrity commented 2 months ago

Thanks for developing this great module @caoxiemeihao I was reliant on it for a couple of my apps, but currently can not use it without downgrading multiple packages which are needed. The --no-fork flag did not end up working for me, but I'm commenting to stay in the loop on hopefully any future updates/fixes.

You also need to downgrade nuxt package to 3.6.5

As I mentioned, I am using features in both Nuxt and other libraries that depend on newer versions. Thank you for the reply though.

Hauskrampf commented 2 months ago

I also encountered this loop problem, tried @angelhdzmultimedia's bun solution with @caoxiemeihao extra flag, which worked once, although the DevTools where broken. Any builds since then just spits out, in the electron window:

JavaScript execution terminated.

Stack trace:


  λ()
  λ()
  λ()
  λ()
  λ()
  λ()
  λ()
  λ()

Info:

bun v1.1.4 wsl #1 SMP Thu Oct 5 21:02:42 UTC 2023 (x64) User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) nuxt-app/0.0 Chrome/124.0.6367.60 Electron/30.0.1 Safari/537.36 Pathname: /

and the console prints:

error: JavaScript execution terminated.
GET - / failed

I then tried to remove the --no-fork flag, which now successfully builds sometimes? Other times i get:

519 |       return this.emit(
520 |         "error",
521 |         new Error("Must provide a proper URL as target")
522 |       );
523 |     }
524 |     let _resolve;
             ^
error: Must provide a proper URL as target
      at /home/[etc pp]/node_modules/httpxy/dist/index.mjs:524:8
      at /home/[etc pp]/node_modules/nuxi/dist/chunks/dev2.mjs:6902:29
      at /home/[etc pp]/node_modules/nuxi/dist/chunks/dev2.mjs:6891:21
      at emit (node:events:158:48)
      at fetch (node:http:483:12)
error: script "dev" exited with code 1

and the DevTools are still broken.

The sheer randomness of this bug baffles me....

TLDR: If you don't want to downgrade nuxt, try bun --bun run dev, it may work ¯_(ツ)_/¯

angelhdzmultimedia commented 2 months ago

I also encountered this loop problem, tried @angelhdzmultimedia's bun solution with @caoxiemeihao extra flag, which worked once, although the DevTools where broken. Any builds since then just spits out, in the electron window:

JavaScript execution terminated. Stack trace:

  λ()
  λ()
  λ()
  λ()
  λ()
  λ()
  λ()
  λ()

Info:

bun v1.1.4 wsl #1 SMP Thu Oct 5 21:02:42 UTC 2023 (x64) User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) nuxt-app/0.0 Chrome/124.0.6367.60 Electron/30.0.1 Safari/537.36 Pathname: /

and the console prints:

error: JavaScript execution terminated.
GET - / failed

I then tried to remove the --no-fork flag, which now successfully builds sometimes? Other times i get:

519 |       return this.emit(
520 |         "error",
521 |         new Error("Must provide a proper URL as target")
522 |       );
523 |     }
524 |     let _resolve;
             ^
error: Must provide a proper URL as target
      at /home/[etc pp]/node_modules/httpxy/dist/index.mjs:524:8
      at /home/[etc pp]/node_modules/nuxi/dist/chunks/dev2.mjs:6902:29
      at /home/[etc pp]/node_modules/nuxi/dist/chunks/dev2.mjs:6891:21
      at emit (node:events:158:48)
      at fetch (node:http:483:12)
error: script "dev" exited with code 1

and the DevTools are still broken.

The sheer randomness of this bug baffles me....

TLDR: If you don't want to downgrade nuxt, try bun --bun run dev, it may work ¯(ツ)

The reason it works with --bun flag is it's running with Bun not Node. The bug seems to be related to higher versions of Nuxt + Node. Hope this is fixed soon. I stopped working on my electron app due to this.

rvalledorjr commented 1 month ago

Encountered the same issue on a new project and able to resolve it by updating the nuxt.config.ts to:

// Only relevant configs are shown
export default defineNuxtConfig({
  modules: [
    (options, nuxt) => {
      if (!nuxt.options.dev) {
        nuxt.options.nitro.runtimeConfig ??= {}
        nuxt.options.nitro.runtimeConfig.app ??= {}
        nuxt.options.nitro.runtimeConfig.app.baseURL = './'
      }
    },
    'nuxt-electron',
  ],
  electron: {
    disableDefaultOptions: true,
  },
  ssr: false,
  router: {
    options: {
      hashMode: true
    }
  },
})

NOTE There are times on initial page load (dev mode), a 404 or 500 error will occur. This will clear up after you do a page refresh.

petaxa commented 3 weeks ago

The reason it works with --bun flag is it's running with Bun not Node. The bug seems to be related to higher versions of Nuxt + Node. Hope this is fixed soon. I stopped working on my electron app due to this.

Could it be that the reason is because the Nuxt CLI handles Bun specifically? Looking at the implementation of the Nuxt CLI, it seems that Bun is treated the same as --no-fork by default.

https://github.com/nuxt/cli/blob/03d4a543f0d8c029d68c5510560c11e4a457802d/src/commands/dev.ts#L22

Sorry if this doesn't seem relevant to you, but I mention for helping someone who comes here.