【问题标题】:Change the setTimeout() function from JSDOM to the node.js default?将 setTimeout() 函数从 JSDOM 更改为 node.js 默认值?
【发布时间】:2021-03-26 01:47:08
【问题描述】:

我在使用带有 ytdl-core 和电子包的 node.js 时,在使用 ytdl.getInfo() 函数时出现错误。该错误仅在电子渲染器文件中调用 .getInfo() 函数时发生,在主文件中它工作正常,所以我确定它与脚本链接到的 html 文件有关。

错误消息是这样的,并且多次出现:

Uncaught TypeError: setTimeout(...).unref is not a function
    at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
    at Function.exports.<computed> [as getInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
    at down.js:3
C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12 Uncaught (in promise) TypeError: setTimeout(...).unref is not a function
    at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
    at Object.exports.<computed> [as getBasicInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
    at exports.getInfo (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:352)
    at C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:27)
    at Function.exports.<computed> [as getInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
    at down.js:3
C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12 Uncaught (in promise) TypeError: setTimeout(...).unref is not a function
    at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
    at getHTMLWatchPageBody (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:113)
    at C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:127
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:27)
    at getIdentityToken (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:126)
    at setIdentityToken (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:266)
    at getWatchJSONPage (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:277)
    at async retryFunc (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:207)
    at async pipeline (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:150)

我做了一些研究,它似乎有什么东西。因为我的程序使用的是 JSDom setTimeout() 函数而不是 node.js 版本。我不太了解那些在 GitHub 上争论的人,他们有类似的问题。所以我问我如何让它使用 node.js setTimeout() 函数。我创建了一个 2. 节点项目并重现了该问题。

这里是一些我不太明白发生了什么的 GitHub 链接:

https://github.com/electron/electron/issues/21162

https://github.com/sindresorhus/get-port/pull/40

https://github.com/facebook/jest/issues/9033

https://github.com/grpc/grpc-node/issues/1077

https://github.com/facebook/jest/issues/1909

https://github.com/jsdom/jsdom/blob/a6acac4e9dec4f859fff22676fb4e9eaa9139787/lib/jsdom/browser/Window.js#L176

这是我来自 2. 项目的代码:

main.js,我只是从电子端复制过来的

const { app, BrowserWindow } = require('electron');

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
  win.openDevTools();
}

app.whenReady().then(createWindow)

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

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})

我的 index.html 文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
    <h1>Hello World!</h1>
    <p>
        We are using node <script>document.write(process.versions.node)</script>,
        Chrome <script>document.write(process.versions.chrome)</script>,
        and Electron <script>document.write(process.versions.electron)</script>.
    </p>
    <script src="down.js"></script>
</body>
</html>

我的下载器文件:

const ytdl = require("ytdl-core");

ytdl.getInfo("https://www.youtube.com/watch?v=ZA6LEWQf6us").then(
      (info) => {
          console.log(info);
      }
);

我的 package.json 文件:

{
  "name": "nodetestenv",
  "version": "1.0.0",
  "description": "test node things",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^11.1.0"
  },
  "dependencies": {
    "ytdl-core": "^4.1.5"
  }
}

谢谢。

【问题讨论】:

    标签: javascript node.js electron jsdom


    【解决方案1】:

    我找到了一个有用的来源。启动电子时,有一个面板问题,这似乎是新的,我在其中找到了一个指向 this 网站的链接,解释说我的问题来自 Content Securitz 政策。在我看到这个错误之后,我的 node.js 应用程序也告诉我,ytdl-core 有一个新的更新。我认为昨天不存在,更新解决了我的问题。

    这是我的项目中的一张照片,其中包含错误和消息。这是我的实际项目,因为我在测试环境中找到了修复程序。 Picture of Error

    【讨论】:

      猜你喜欢
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2019-09-03
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 2016-11-24
      相关资源
      最近更新 更多