尝试包含 ytdl-core 时找不到模块“http”或其相应的类型声明

我为运行 npm install ytdl-core --save ( https://www.npmjs.com/package/ytdl-core ) 的 Angular 项目下载 ytdl-core。为了使用这个模块,我这样做:

import ytdl from 'ytdl-core';

ytdl(url);

当我启动 Angular 应用程序时,它会显示下一条消息:

ERROR in ./node_modules/miniget/dist/index.js
Module not found: Error: Can't resolve 'http' in 'D:workspacescustomernode_modulesminigetdist'

ERROR in ./node_modules/miniget/dist/index.js
Module not found: Error: Can't resolve 'https' in 'D:workspacescustomernode_modulesminigetdist'

    ERROR in node_modules/ytdl-core/typings/index.d.ts:2:33 - error TS2307: Cannot find module 'http' or its corresponding type declarations.
2   import { ClientRequest } from 'http';
                                  ~~~~~~

问题是什么?

stack overflow Cannot find module 'http' or its corresponding type declarations while I try include ytdl-core
原文答案

答案:

作者头像

这是由浏览器中不可用的节点模块的依赖关系引起的。将这些添加到您的 package.json 文件中。

"browser": {
  "http": false,
  "https": false,
  "net": false,
  "path": false,
  "stream": false,
  "tls": false
}

删除你的 node_modules 文件夹并运行这些命令

npm install
npm install stream
npm install --save-dev @types/node
npm install ytdl-core --save

转到您的 node_modules/ytdl-core/typings 文件夹并从 index.d.ts 中删除这些行

import { ClientRequest } from 'http'; // Delete this

enter image description here