我正在使用这两个库/模板:
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'
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/