NodeJS 使用网络包操纵 puppet

wmtdaxz3  于 2022-12-22  发布在  Node.js
关注(0)|答案(2)|浏览(142)

我怎样才能用网络包来使用 puppet 师?无论我怎么试它都找不到。这是我所知道的:
依赖项安装在包. json中:

npm i puppeteer

In webpack.dev:

module.exports = {
    // ... I'm skipping over a bunch of stuff here, then:
    externals: {
        puppeteer: require('puppeteer'),
    }
}\\

然后在客户端的入口文件index.js中:

import puppeteer from 'puppeteer';

运行后立即:

puppeteer.launch();

只得到这个错误:

Uncaught TypeError: Cannot read property 'launch' of undefined

我做错了什么?我总是在webpack中使用外部插件。
快速更新,我尝试了webpack. providePlugin,并与jquery库并行测试,jquery遵循相同的逻辑工作,但puppeteer仍然给我一个错误:
In webpack.dev:

plugins: [
    new webpack.ProvidePlugin({
        puppeteer: 'puppeteer',
        jQuery: 'jquery'
    })

在index.js中:

import 'puppeteer'
import 'jquery'

然后我得到一堆错误,如:

/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\victo\OneDrive\Desktop\20200307-kino\kino\node_modules\puppeteer\lib\cjs\puppeteer\node'
errors @ client:159
eval @ socket.js:47
sock.onmessage @ SockJSClient.js:67
EventTarget.dispatchEvent @ sockjs.js:170
eval @ sockjs.js:888
SockJS._transportMessage @ sockjs.js:886
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2962
client:159 ./node_modules/extract-zip/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\victo\OneDrive\Desktop\20200307-kino\kino\node_modules\extract-zip'
hzbexzde

hzbexzde1#

添加到您的配置如下。这在我的情况下工作。注解掉的可能也适用

module.exports = {
// ... 
node: {
  child_process: 'empty',
  // net: 'empty',
  // dns: 'empty',
  // tls: 'empty',
  // readline: 'empty'
}
}
yeotifhr

yeotifhr2#

我确实遇到过类似的问题,而使用webpack的节点应用程序与puppeteer作为一个依赖项。

module.exports = {
    // other conf
    externals: {
        puppeteer: "require('puppeteer')", // use additional double quotes here to define valid external
    }
}

应该能胜任。

相关问题