Skip to content

[Feature] 如何兼容 Node 16 #44

Open

Description

@code-lixm

Desc

我的使用场景是在 electron中有node 进程启动服务,但是我的electron需要兼容 window7/8.1,所以electron的版本需要低于V22.但是此项目 devtool的要求是 node 18+,于是我想升级electron 内核支持 node18+,需要升级到 electron v23,但是在 electron 升级到 23 版本后,不再兼容 win7/8.1
具体链接:Electron 23.0.0

To Resolve?

能不能降低 API 版本以兼容 node16
...

Resolutions(Optional)

...

Activity

  1. GrinZero commented on Sep 30, 2025

    @GrinZero
    Owner

    哈喽,我会在今天晚上尝试兼容~当然如果可以,可以给一些错误截图

  2. code-lixm commented on Sep 30, 2025

    @code-lixm
    Author

    感谢
    ReferenceError: ReadableStream is not defined
    第一个问题已解决,ReadableStream defined in node V18

    function ensureWebStreamsAPI() {
      if (typeof global.ReadableStream === 'undefined') {
        const { ReadableStream, WritableStream, TransformStream } = require('node:stream/web')
        global.ReadableStream = ReadableStream
        global.WritableStream = WritableStream
        global.TransformStream = TransformStream
      }
    }

    也有可能是第一个问题的解决方式,导致出现了第二个问题
    Unhandled RangeError: Invalid WebSocket frame: RSV1 must be clear
    这个问题是 ws 的问题,猜测问题可能出现在 permessage-deflate
    我的 electron 版本是 15.5.7,对应内核在 node V16.5

  3. GrinZero commented on Sep 30, 2025

    @GrinZero
    Owner

    脑测一下,试试 require('node:stream')

  4. code-lixm commented on Sep 30, 2025

    @code-lixm
    Author

    脑测一下,试试 require('node:stream')

    Unhandled TypeError [ERR_INVALID_ARG_TYPE]: The "list[0]" argument must be an instance of Buffer or Uint8Array. Received type string ('ok')
    现在出现了这个报错,现在一步一个脚印,这个错误一般是因为什么

  5. GrinZero commented on Sep 30, 2025

    @GrinZero
    Owner

    看起来是这里出错了,还是跟 websocket 有关系 ,要看看你那里是尝试用 ws 连接哪里了;

    Image

    晚点我切到 16 测一下

  6. GrinZero commented on Oct 15, 2025

    @GrinZero
    Owner

    跟你同步一下进度。
    我尝试了

    function ensureNode16() {
      if (typeof global.ReadableStream === 'undefined') {
        const { ReadableStream, WritableStream, TransformStream } = require('node:stream/web')
        global.ReadableStream = ReadableStream
        global.WritableStream = WritableStream
        global.TransformStream = TransformStream
      }
    }
    

    出现的报错是:

    Image

    所以我简单 patch 了一下 ws:

        module.exports.isValidUTF8 = function (buf) {
          const curIsValidUTF8 = typeof isValidUTF8 === 'function' ? isValidUTF8 : isValidUTF8.Validation.isValidUTF8;
          return buf.length < 32 ? _isValidUTF8(buf) : curIsValidUTF8(buf);
        };
    

    这样就可以运行了。
    我这里是 v16.20.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions