【问题标题】:How to exclude by webpack.config.js, or fix this problem如何通过 webpack.config.js 排除,或者修复这个问题
【发布时间】:2022-01-03 01:04:17
【问题描述】:

我正在使用 react create app 来制作一个网站,我几乎完成了我的代码, 但是在我导入'puppeteer'之后,出现了很多错误。 例如

ERROR in ./node_modules/yauzl/index.js 13:16-43
Module not found: Error: Can't resolve 'stream' in 'C:\Users\User\Documents\VSCode\lubnx\node_modules\yauzl'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
 @ ./node_modules/extract-zip/index.js 19:14-30
 @ ./node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js 75:38-60
 @ ./node_modules/puppeteer/lib/cjs/puppeteer/node/Puppeteer.js 31:28-58
 @ ./node_modules/puppeteer/lib/cjs/puppeteer/initialize-node.js 29:23-53
 @ ./node_modules/puppeteer/lib/cjs/puppeteer/node.js 22:29-60
 @ ./node_modules/puppeteer/cjs-entry.js 28:24-59
 @ ./src/bsc.js 3:18-38
 @ ./src/App.js 9:0-24
 @ ./src/index.js 7:0-24 13:35-38

90 errors have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

webpack 5.65.0 compiled with 90 errors and 58 warnings in 2608 ms

有很多错误,我不知道。 我爬了一些网站,但还是一头雾水,我尝试制作一个 webpack.config.js,但它似乎不起作用。

const path = require('path');
module.exports = {
    entry: [
      './index.js'
    ],
    output: {
      path: path.join(__dirname,"public"),
      filename: 'bundle.js'
    },
    module: {
        rules:[
            {
                test: /\.yauzl$/,
                exclude: /\.js$/,
            }
        ]
    }
  }; 

这是我的傀儡代码

const puppeteer = require('puppeteer');
const dappeteer = require('@chainsafe/dappeteer');

const seed = 'quote violin crane pride emotion cart pyramid grunt custom release work sauce';
const mining = 'https://www.binaryx.pro/#/game/work?workType=partTime&work=2';

const bsc = async()=> {
  const browser = await dappeteer.launch(puppeteer, { metamaskVersion: 'v10.1.1' });

  const metamask = await dappeteer.setupMetamask(browser, {
    seed: seed,
    password: 'pass1234',
});

  await metamask.addNetwork({
    networkName: 'BSC',
    rpc: 'https://bsc-dataseed1.binance.org/',
    chainId: 56,
    symbol: 'BNB',
    explorer: 'https://bscscan.com/',
  });

  await metamask.switchNetwork('BSC');

  const page = await browser.newPage();
  await page.goto(mining, { waitUntil: 'networkidle2' });
  await metamask.approve();
  await new Promise(resolve => setTimeout(resolve, 7000));
  return (
    await page.evaluate(async() => {
      const mn = document.querySelector('#app > section > main > div > div.work-page > div.job-record > div.flex-between.record-title--wrap > span > div');
      const res = mn.innerText;
      return res;
    })
  )
}
bsc();

导出默认 bsc;

【问题讨论】:

  • 您到底想在这里做什么,您需要将 Puppeteer 之类的东西与 webpack 捆绑在一起......?你能用你自己的话解释一下你用 Puppeteer 做什么吗?
  • 我正在尝试爬取另一个网站,所以我需要 puppeteer,当我在我的代码中导入“puppeteer”并运行 npm start 时,我会收到这些错误。
  • Puppeteer 不是设计为在浏览器中运行。你为什么要这样使用它?
  • 这能回答你的问题吗? How to run Puppeteer code in any web browser?

标签: javascript node.js webpack


【解决方案1】:

正如 Webpack 建议的那样,您可以通过添加后备 (resolve.fallback: { "stream": require.resolve("stream-browserify") }) 来包含 stream polyfill:

const path = require('path');
module.exports = {
    entry: [
      './index.js'
    ],
    output: {
      path: path.join(__dirname,"public"),
      filename: 'bundle.js'
    },
    resolve: {
        fallback: {
            "stream": require.resolve("stream-browserify");
        }
    }
    module: {
        rules:[
            {
                test: /\.yauzl$/,
                exclude: /\.js$/,
            }
        ]
    }
  }; 

并安装stream-browserify:

npm i stream-browserify

你可以阅读更多关于resolve.fallbackhere的信息。


【讨论】:

  • 我和你的一样,但错误仍然发生。常量路径 = 要求('路径'); module.exports = { entry: [ './index.js' ], output: { path: path.join(__dirname,"public"), filename: 'bundle.js' }, resolve: { fallback: { "stream “:require.resolve(“stream-browserify”),“fs”:require.resolve(“stream-browserify”),}},模块:{规则:[{测试:/\.yauzl$/,排除:/ \.js$/, } ] } };
  • @huihui 这很有趣...我可以看看你的完整代码(或存储库吗?)
  • 我没有……好像看到webpack.config.js
  • 哦,我可能忘记推送最新版本了,但我照我发的那样做了。
猜你喜欢
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2020-05-06
  • 2021-01-30
  • 2021-01-09
相关资源
最近更新 更多