javascript - 在 Vue + Webpack 中使用 Puppeteer (Headless Chrome) 时的依赖错误

标签 javascript node.js webpack vue.js puppeteer

我正在使用这两个库/模板:

https://github.com/GoogleChrome/puppeteer ( headless Chrome)

https://github.com/vuejs-templates/pwa (使用 Webpack 和 Express 的 Vue 模板)。

这是代码:

export default {
  mounted () {
    const puppeteer = require('puppeteer')

    ;(async () => {
      const browser = await puppeteer.launch()
      const page = await browser.newPage()
      await page.goto('https://www.google.com/search?tbm=bks&q=%22this+is%22')
      const result = await page.evaluate(() => {
        const stats = document.querySelector('#resultStats')
        return stats.textContent
      })
      console.log(result)
      await browser.close()
    })()
  }
}

我做了很多次 npm install 但我仍然有依赖错误:

These dependencies were not found:

  • child_process in ./node_modules/puppeteer/lib/Launcher.js, ./node_modules/puppeteer/node6/Launcher.js
  • fs in ./node_modules/extract-zip/index.js, ./node_modules/extract-zip/node_modules/mkdirp/index.js and 18 others

To install them, you can run: npm install --save child_process fs Listening at http://localhost:8080

Node :我也做了很多次npm install --save child_process fs。同样的错误。

最佳答案

这似乎是 webpack 对内置 Node 模块的提示。尝试将以下内容添加到您的 webpack 配置中...

target: 'node'

from the webpack docs...

In the example above, using node webpack will compile for usage in a Node.js-like environment (uses Node.js require to load chunks and not touch any built in modules like fs or path).


或者,您可以使用以下方法解决此问题...

node: {
  fs: 'empty',
  child_process: 'empty'
}

关于javascript - 在 Vue + Webpack 中使用 Puppeteer (Headless Chrome) 时的依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203381/

相关文章:

javascript - 如何使用 webpack 配置按文件夹划分的模块 bundler ?

javascript - jQuery.parseJSON() 无效字符错误

javascript - 如何在xmlhttprequest中发送jwt?

javascript - 即使服务器/客户端位于同一时区,日期输入提要也是错误的

node.js - 只允许通过 nginx 和 docker 访问express app

javascript - 在 Node-Gulp-Webpack 构建中包含外部 JavaScript 文件

javascript - 离线应用程序上的代码分割

javascript - Bootstrap-Vue : @click. native 在 <b-form-checkbox> 上表现得很奇怪

javascript - 选择一个选项时的 jquery addClass 和 removeClass

javascript - 无法在 Sails JS 中创建版本化 API