【发布时间】: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
这是我来自 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